在实体框架中设置默认值 - 数据库优先 [英] Setting default value in entity framework - Database First

查看:213
本文介绍了在实体框架中设置默认值 - 数据库优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework 4.3.1,其中包含数据库中的自动生成的实体。



从这里,是否有任何方法将默认值设置为? X- 2004545 CEEC X-



我明白可以使用部分类,所以我尝试了这样的一个实体,生成实体,DESCRIPTION_是我要设置为默认的属性

 命名空间名称。模型
{
公共部分类ENTITY
{
public string DESCRIPTION_
{
set {_DESCRIPTION_ =default string; }
}
}
}

也许如果有人可以给我的例子很棒!

解决方案

你给出的例子意味着DESCRIPTION只能是默认字符串 p>

您可以在构造函数中设置它

 命名空间name.Models 
{
public partial class ENTITY
{
private string defaultDescription =some text;新评新新新新新新新旗新新旗新新旗新新旗200新新新新旗新新旗200新新新新旗新新旗新新旗新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新
}
}
}

或将您的财产切换为一个有一个后备字段

  namespace name.Models 
{
public partial class ENTITY
{
private string _desc =some default value;
public virtual string DESCRIPTION_ {get {return _desc} set {_desc = value;}}
}
}


I am using Entity Framework 4.3.1, with auto-generated entities from the database.

From this, is there any way to set the default value to something? I don't want to put it in the auto-generated code since it will be overwritten.

I understand that it is possible to use partial classes, so I tried something like this, where entity is generated, and DESCRIPTION_ is the attribute I want to set to a default value.

namespace name.Models
{
    public partial class ENTITY
    {
        public string DESCRIPTION_
        {
            set { _DESCRIPTION_ = "default string"; }
        }
    }
}

Maybe if somebody could give me an example that would be great!

解决方案

The example you give means that DESCRIPTION can only ever be "default string"

You can set it in the constructor

namespace name.Models 
{
  public partial class ENTITY
  {
    private string defaultDescription = "some text";
    public ENTITY() {
      DESCRIPTION_ = defaultDescription;
    } 
  }
}

or by switching your property to one with a backing field

namespace name.Models
{
    public partial class ENTITY
    {
        private string _desc = "some default value"; 
        public virtual string DESCRIPTION_ {get {return _desc} set {_desc = value;} }
    }
}

这篇关于在实体框架中设置默认值 - 数据库优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆