1

I'm implementing user's language setting to store it in database:

[Table("User")]
public class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string Language { get; set; }

    [NotMapped]
    public CultureInfo Culture => string.IsNullOrWhiteSpace(Language) ? Thread.CurrentThread.CurrentCulture : new CultureInfo(Language);
}

Is it possible to specify Language values can be? For example: "sl-SI" or "hr-HR" or "ru-RU" only? Thanks in advance!

2 Answers 2

3

Maybe you can create an enum of supported languages and use helper for strings:

public enum SupportedLanguages
{
    [Description("sl-SL")]
    Sl,
    [Description("hr-HR")]
    Hr,
    [Description("ru-RU")]
    Ru
}

public static string GetDescription(this Enum value)
{            
    FieldInfo field = value.GetType().GetField(value.ToString());

    DescriptionAttribute attribute
            = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
                as DescriptionAttribute;

    return attribute == null ? value.ToString() : attribute.Description;
}

Then use this like this

[Table("User")]
public class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    private SupportedLanguages language;
    public SupportedLanguages Language 
    { 
        get { return language; }
        set 
        {
            if(!Enum.IsDefined(typeof(SupportedLanguages), value))
                throw new ArgumentOutOfRangeException();
            language = value;
        }
    }

    [NotMapped]
    public CultureInfo Culture => string.IsNullOrWhiteSpace(Language.GetDescription()) ? Thread.CurrentThread.CurrentCulture : new CultureInfo(Language.GetDescription());
}
Sign up to request clarification or add additional context in comments.

8 Comments

What about ctx.Database.ExecuteSqlCommand("insert into Users(Language) values (22)") and user.Language = (SupportedLanguages)22?
Are you sure, you can try it by yourself
You can't assign an invalid integer value to a enum field. The whole idea behind enum is this. Also you can invalidate database in many ways manually but if you use EF all the way you don't need to worry about it
@SlavaUtesinov Sure I forgot that in C# enums are not checked! and it's for Flags capability. I have edited the answer see the new one.
Improving my code I think it would be better to make SupportedLanguages property required and remove extra validation from get method of Culture property.
|
0

You should do it at migration:

public override void Up()
{  
    Sql("ALTER TABLE Users ADD CONSTRAINT LanguageCnst CHECK (Language in ('sl-SI', 'hr-HR', 'ru-RU'))");
}

public override void Down()
{
    Sql('ALTER TABLE Users DROP CONSTRAINT LanguageCnst');
}

And add some code to User:

[Table("User")]
public class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string Language { get; protected set; }

    public setLanguage(string value)
    {
        if(new[]{"sl-SI", "hr-HR", "ru-RU"}.Contains(value))
            Language = value;
    }

    [NotMapped]
    public CultureInfo Culture => string.IsNullOrWhiteSpace(Language) ? Thread.CurrentThread.CurrentCulture : new CultureInfo(Language);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.