C# EntityFramework 保存 1-1 关系
我在 Employee
和 EmployeePermission
实体之间存在一对一关系,分别具有导航属性 PermissionTo
和 Employee
。我可以很好地加载导航属性,但我遇到的问题是,当我更改相关实体 EmployeePermission
上的属性时,将 Employee
实体附加到数据库上下文并调用 SaveChanges()
方法,EmployeePermission
实体中的更改不会持久化到数据库,但 Employee
实体中的更改会被持久化。
这是我的模型的样子:
这是我的保存函数的样子:
public static void SaveEntities(List<TEntity> entities)
{
using(var db = GetContext())
{
ObjectSet<TEntity> table = db.CreateObjectSet<TEntity>();
foreach(TEntity entity in entities)
{
if (entity.IsNew())
table.AddObject(entity);
else
{
table.Attach(entity);
EntityState state = (entity.EntityKey.IsTemporary) ? EntityState.Added : EntityState.Modified;
table.Context.ObjectStateManager.ChangeObjectState(entity, state);
}
}
db.SaveChanges();
}
}
IsNew()
是我添加到 EntityObject 类的扩展方法,用于检查实体是否必须添加或附加到 EntitySet 。
/// <summary>
/// Returns a value indicating if the entity object is new.
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public static bool IsNew(this EntityObject entity)
{
return (entity.EntityKey == null || entity.EntityKey.IsTemporary);
}
我加载一名员工并将其属性绑定到表单上,更改它们并调用 SaveEntities()
方法。对 Employee 实体的所有更改都保存到数据库中,但是对 EmployeePermission 实体的更改没有保存到数据库中,并且我还没有获取其他实体尚未。
我不确定我做错了什么并且已经搜索了几个小时。我感谢所有的帮助,提前致谢。
I have a one-to-one relationship between the Employee
and EmployeePermission
entities with navigation properties PermissionTo
and Employee
respectively. I can load the navigation properties fine, but the problem I'm having is that when I change properties on the related entity EmployeePermission
, attach the Employee
entity to a database context and call the SaveChanges()
method, the changes in the EmployeePermission
entity are not being persisted to the database, but the changes in the Employee
entity are.
Here's what my model looks like:
Here's what my save function looks like:
public static void SaveEntities(List<TEntity> entities)
{
using(var db = GetContext())
{
ObjectSet<TEntity> table = db.CreateObjectSet<TEntity>();
foreach(TEntity entity in entities)
{
if (entity.IsNew())
table.AddObject(entity);
else
{
table.Attach(entity);
EntityState state = (entity.EntityKey.IsTemporary) ? EntityState.Added : EntityState.Modified;
table.Context.ObjectStateManager.ChangeObjectState(entity, state);
}
}
db.SaveChanges();
}
}
IsNew()
is an extension method I added to the EntityObject
class to check if the Entity has to be added or attached to the EntitySet
.
/// <summary>
/// Returns a value indicating if the entity object is new.
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public static bool IsNew(this EntityObject entity)
{
return (entity.EntityKey == null || entity.EntityKey.IsTemporary);
}
I load an employee and bind its properties on a form, change them and call the SaveEntities()
method. All the changes to the Employee
entity are saved to the database, but the changes to the EmployeePermission
entity are not saved to the database, and I haven't gotten to the other entities yet.
I'm not sure what I'm doing wrong and have been searching for hours. I appreciate all the help, thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自从您将
EmployeePermission
记录添加到上下文以来,您似乎没有更改它。由于 EF 不知道它已被修改,因此它不知道有什么需要保存。如果您愿意,也可以对该对象调用ChangeObjectState
。Doesn't sound like you've changed the
EmployeePermission
record since you added it to the context. Since the EF doesn't know it's modified, it's unaware there's anything to save. You can callChangeObjectState
on that object, as well, if you care to.