模型属性初始化

时间:2013-11-30 16:15:26

标签: c# entity-framework asp.net-mvc-4 code-first

我有这个模型类“UserProfile”,它是原始的UserProfile类的Membership,带有一些添加的属性和方法。

[Table("UserProfile")]
public class UserProfile
{

   public UserProfile()
   {
       this.DictionaryFrom = "eng";
       this.DictionaryTo = "hun";
       this.trainingType = "normal";
   }


    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string DictionaryFrom { get; set; }
    public string DictionaryTo { get; set; }
    public string trainingType { get; set; }

    public virtual ICollection<ForeignExpression> learnedexpressions { get ; set ; }
}

我的问题是,在注册新用户时,构造函数中的三个字段不会获取分配给它们的值(因此,数据库中的每个字段都有一个NULL)。 用户可以通过从列表中选择值来设置它们,但我想为所有这些值设置默认值。我做错了什么?

1 个答案:

答案 0 :(得分:0)

不是C#爱好者,我会做这样的事情......这可能是一种“更好”的做法。

private string myValue = "default value";

public string MyValue {
    get { return myValue; }
    set {
        if (null != value) { myValue = value; }
    }
}