Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 模型属性初始化_C#_Entity Framework_Asp.net Mvc 4_Code First - Fatal编程技术网

C# 模型属性初始化

C# 模型属性初始化,c#,entity-framework,asp.net-mvc-4,code-first,C#,Entity Framework,Asp.net Mvc 4,Code First,我有一个模型类“UserProfile”,它是原始的UserProfile成员类,添加了一些属性和方法 [Table("UserProfile")] public class UserProfile { public UserProfile() { this.DictionaryFrom = "eng"; this.DictionaryTo = "hun"; this.trainingType = "normal"; }

我有一个模型类“UserProfile”,它是原始的UserProfile成员类,添加了一些属性和方法

[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 ; }
}
[表(“用户配置文件”)]
公共类用户配置文件
{
公共用户配置文件()
{
此字段为。DictionaryFrom=“eng”;
this.DictionaryTo=“hun”;
this.trainingType=“正常”;
}
[关键]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId{get;set;}
公共字符串用户名{get;set;}
来自{get;set;}的公共字符串字典
公共字符串字典{get;set;}
公共字符串训练类型{get;set;}
公共虚拟ICollection learnedexpressions{get;set;}
}
我的问题是,在注册新用户时,构造函数中的三个字段没有得到分配给它们的值(因此,数据库中每个字段都有一个空值)。 用户可以通过从列表中选择值来设置它们,但我想为所有这些设置一个默认值。我做错了什么?

不是一个C#迷,我会做这样的事。。。可能有一种“更好”的方法

private string myValue = "default value";

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

您是否在保存之前意外地将值置乱了?@ohiodoug我不这么认为,构造函数中的代码似乎在创建新用户的过程中运行,但对数据库没有任何影响。您是否在表单中包含这些字段并允许用户将其未填充发布?@dombenoit No,我使用的是默认的会员注册页面(/Account/Register),没有任何修改。您也可以显示视图和控制器GET/POST方法吗?