[英]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.下面你可以看到我尝试过的东西——它确实创建了一个默认值——但即使数据库中有东西,它也总是返回默认对象。
Profile
is null
I want to return a default initialzied objectProfile
为null
我想返回一个默认的初始化对象Profile
is not null
I want to return the object from the databaseProfile
不为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.