简体   繁体   English

为实体框架导航属性设置默认对象

[英]Setting a default object for Entity Framework navigation property

Is it possible to set a default object for an Entity?是否可以为实体设置默认对象?

Say I have a Person entity that originally did not have a requirement for a Profile .假设我有一个Person实体,它最初对Profile没有要求。

Now I require a Profile - but there are existing entities that do not currently have a Profile .现在我需要一个Profile - 但是存在当前没有Profile现有实体。

Is there a way I can provide a default object for these Entities when they're loaded in future, so anyone using the Person Entity can assume that Profile is never null and always has a value - even if it's a default.有没有办法在将来加载这些实体时为这些实体提供默认对象,因此任何使用Person实体的Person都可以假设Profile永远不会为 null 并且始终具有值 - 即使它是默认值。

Below you can see what I've tried - which does create a default value - but even when there is something in the database it always return the default object.下面你可以看到我尝试过的东西——它确实创建了一个默认值——但即使数据库中有东西,它也总是返回默认对象。

  • If the Profile is null I want to return a default initialzied object如果Profilenull我想返回一个默认的初始化对象
  • If the Profile is not null I want to return the object from the database如果Profile不为null我想从数据库中返回对象

Additionally - what would be the most sensible way to attach the "default" object to my dbcontext?另外 - 将“默认”对象附加到我的 dbcontext 的最明智的方法是什么?

How can I achieve this desired behavior?我怎样才能实现这种理想的行为?

public class Person
{
    [Key]
    public int Id {get; set;}

    private Profile _profile;
    public virtual Profile Profile
    {
        get
        {
            return _profile ?? (_profile= new Profile
            {
                Person = this,
                PersonId = Id
            });
        }
        set
        {
            _profile = value;
        }

        // properties
    }
}

public class Profile
{
    [Key, ForeignKey("Person")]
    public int PersonId {get; set;}

    [ForeignKey("PersonId")]
    public virtual Person Person{ get; set; }

    // properties
}

I know you can initialize collections so that they're not null, but I'd like to initialize a single object too.我知道您可以初始化集合以使它们不为空,但我也想初始化一个对象。

Use the ObjectContext.ObjectMaterialized Event .使用ObjectContext.ObjectMaterialized 事件

This event is raised for each entity that is being loaded into context after materialization.为实现后加载到上下文中的每个实体引发此事件。

In the constructor for your context, subscribe to this event.在上下文的构造函数中,订阅此事件。 And in the event handler, check if the entity type is Person , and if so, create the new profile for the person.在事件处理程序中,检查实体类型是否为Person ,如果是,则为该人创建新配置文件。 Here is a code sample:这是一个代码示例:

public class Context : DbContext
{
    private readonly ObjectContext m_ObjectContext;

    public DbSet<Person> People { get; set; }
    public DbSet<Profile> Profiles { get; set; }

    public Context()
    {
        var m_ObjectContext = ((IObjectContextAdapter)this).ObjectContext;

        m_ObjectContext.ObjectMaterialized += context_ObjectMaterialized;

    }

    void context_ObjectMaterialized(object sender, System.Data.Entity.Core.Objects.ObjectMaterializedEventArgs e)
    {

        var person = e.Entity as Person;

        if (person == null)
            return;

        if (person.Profile == null)
            person.Profile = new Profile() {Person = person};

    }
}

Please note that if you commit changes, the new profiles will be saved back to the database.请注意,如果您提交更改,新配置文件将保存回数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为Entity Framework导航属性创建默认对象 - Creating a default object for Entity Framework navigation property 实体框架按ID发行设置导航属性 - Entity Framework Setting Navigation Property by Id Issue 具有ID和对象的实体框架导航属性失败 - Entity Framework Navigation Property with Id and Object fail 实体框架6:将子对象添加到父级列表与将子级的导航属性设置为父级 - Entity Framework 6: Adding child object to parent's list vs. setting child's navigation property to parent 实体框架导航属性 - Entity Framework navigation property 实体框架设置导航属性为零与零的关联为null - Entity Framework setting navigation property null in zero to one association 默认情况下,如何在数据库中具有Entity Framework导航属性查询过滤器? - How to have Entity Framework navigation property query filter in database by default? 实体框架:使用本地对象设置导航属性(尚未保存到数据库中) - Entity Framework: Set navigation property with a local object (not saved to the database yet) 手动将现有对象分配给Entity Framework导航属性 - Manually assign existing object to Entity Framework navigation property 添加对象后,实体框架不会加载导航属性 - entity framework does not load navigation property after adding object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM