EF - 构造函数中相关实体的默认值 [英] EF - default values for related entities in constructor

查看:461
本文介绍了EF - 构造函数中相关实体的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了各种答案,表明使用实体的构造函数提供默认属性值是很好的做法,因此:

  public class Person 
{
public int Id {get;组; }
public bool IsCool {get;组; }

public Person()
{
this.IsCool = true;
}
}

但是,如果您初始化导航属性...

  public class Parent 
{
public int Id {get;组; }
public bool IsCool {get;组; }
public Child {get;组; }
public int ChildId {get;组; }

public Parent()
{
this.IsCool = false;
this.Child = new Child();
}
}

public class Child
{
public int Id {get;组; }
public int Age {get;组; }

public Child()
{
this.Age = 13;
}
}

...即使我明确加载一个孩子因此:

  var parent = db.Parents.Include(p => p.Child).FirstOrDefault(); 

parent.Child设置为Child(Id = 0,Age = 13)的新实例,这是不需要的。



有没有正确的方法?

解决方案>

不要在构造函数中初始化导航属性,因为它会从数据库中覆盖实例化的EF数据。



如果需要确保孩子被初始化(所以你不会得到 NullReferenceException 抛出你)。您可以使用支持字段:

  //导航属性
public Child Child
{
get {return this.i_Child ?? (this.i_Child = new Child()); }
set {this.i_Child = value;
}

// Backing Field
protected internal virtual ContactDetails i_ContactDetails {get;私人集合}

不需要任何特殊的映射,Entity Framework将自动检测支持字段,它的包装和几乎就好像没有任何后备字段一样(但数据库中的列名称与后缀字段相同,除非您明确命名)。


I have read various answers that indicate that it good practice to use the constructor of an entity to provide default property values, thus:

public class Person
{
    public int Id { get; set; }
    public bool IsCool { get; set; }

    public Person()
    {
        this.IsCool = true;
    }
}

However, if you initialize a navigation property...

public class Parent
{
    public int Id { get; set; }
    public bool IsCool { get; set; }
    public Child { get; set; }
    public int ChildId { get; set; }

    public Parent()
    {
        this.IsCool = false;
        this.Child = new Child();
    }
}

public class Child
{
    public int Id { get; set; }
    public int Age { get; set; }

    public Child()
    {
        this.Age = 13;
    }
}

... then even when I explicitly load a Child thus:

var parent = db.Parents.Include(p => p.Child).FirstOrDefault();

parent.Child is set to a new instance of Child (Id = 0, Age = 13), which is not desired.

Is there a correct way to do this?

解决方案

Don't initialize Navigation Properties in the constructor as it will override materialized EF data from the database.

If you need to make sure Child is initialized (so you won't get NullReferenceException thrown at you). You can use backing-fields:

// Navigation Property
public Child Child
{
    get { return this.i_Child ?? (this.i_Child = new Child()); }
    set { this.i_Child = value; }
}

// Backing Field
protected internal virtual ContactDetails i_ContactDetails { get; private set; }

No need for any special mapping, Entity Framework will automatically detect the backing-field and it's wrapper and will act almost as if there's no backing-field at all (but the column name in the database will be the same of the backing-field, unless you explicitly name it).

这篇关于EF - 构造函数中相关实体的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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