C# EntityFramework 保存 1-1 关系

发布于 2024-12-03 00:16:22 字数 1731 浏览 3 评论 0原文

我在 EmployeeEmployeePermission 实体之间存在一对一关系,分别具有导航属性 PermissionToEmployee 。我可以很好地加载导航属性,但我遇到的问题是,当我更改相关实体 EmployeePermission 上的属性时,将 Employee 实体附加到数据库上下文并调用 SaveChanges() 方法,EmployeePermission 实体中的更改不会持久化到数据库,但 Employee 实体中的更改会被持久化。

这是我的模型的样子:

Model

这是我的保存函数的样子:

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:

Model

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔戏命师 2024-12-10 00:16:22

自从您将 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 call ChangeObjectState on that object, as well, if you care to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文