简体   繁体   English

GraphDiff导致实体的id在saveChanges()之后不会被填充

[英]GraphDiff causing id of entity to not get populated after saveChanges()

When using graphDiff for creating a record the new id isn't getting populated back into the entity after saveChanges() is called. 当使用graphDiff创建记录时,在调用saveChanges()之后,新的id不会被填充回实体。 Its been logged on the github repo here - https://github.com/refactorthis/GraphDiff/issues/144 but the repo doesn't seem to be that active any more so was hoping someone on here would have some insight on how to get this working. 它已经登录了github repo这里 - https://github.com/refactorthis/GraphDiff/issues/144但是回购似乎没那么活跃了所以希望有人在这里会有一些见解如何让这个工作。

How it should work however qualification.Id is always 0 for a create. 它应该如何工作但是qual.Id对于创建总是0。 For an update it's the correct Id. 对于更新,这是正确的ID。

    public int CreateUpdate(Qualification qualification)
    {
        using (var db = new DataContext())
        {
            db.UpdateGraph(qualification);

            db.SaveChanges();

            return qualification.Id;
        }
    }

My current work around is to not use graph diff for creates but this isn't ideal. 我目前的工作是不使用graph diff进行创建,但这并不理想。

    public int CreateUpdate(Qualification qualification)
    {
        using (var db = new DataContext())
        {
            if (qualification.Id == 0)
            {
                db.Entry(qualification).State = EntityState.Added;
            }
            else
            {
                db.UpdateGraph(qualification);
            }

            db.SaveChanges();

            return qualification.Id;
        }
    }

Thanks 谢谢

So I found the solution - https://github.com/refactorthis/GraphDiff/issues/32#issuecomment-34420859 所以我找到了解决方案 - https://github.com/refactorthis/GraphDiff/issues/32#issuecomment-34420859

Importantly "GraphDiff hands you a new instance back" so you need to set the return to your entity. 重要的是“GraphDiff将您的新实例发回给您”,因此您需要设置实体的回报。

    public int CreateUpdate(Qualification qualification)
    {
        using (var db = new DataContext())
        {
            qualification = db.UpdateGraph(qualification);
            db.SaveChanges(qualification.AuditUserId);

            return qualification.Id;
        }
    }

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

相关问题 在调用 SaveChanges() 之前添加实体的访问 id - access id of added entity before SaveChanges() is called 插入后Db.SaveChanges未分配主键ID - 代码优先实体框架 - Db.SaveChanges Not Assigning Primary Key ID After Insert - Code First Entity Framework 在context.SaveChanges()之前获取新创建的实体的数据库ID(IDENTITY COLUMN)。 - Get the database ID (IDENTITY COLUMN) of newly created entity before context.SaveChanges() 使用实体框架保存更改后,本地数据库不更新 - Local database not update after SaveChanges with entity framework SaveChanges实体框架后延迟加载不起作用 - Lazy Loading Not Working After SaveChanges Entity Framework 实体框架从saveChanges中的contex获取用户 - Entity Framework get user from contex in saveChanges EFPersistenceManager.saveChanges之后缺少“ $ id”“ $ type” - “$id” “$type” missed after EFPersistenceManager.saveChanges foreach 中的 SaveChanges(),用于插入 ID - SaveChanges() inside foreach, to get ID inserted 外键SaveSaves()上的外键ID和描述-Entity Framework - Foreign key id and description on SaveChanges() overide - Entity Framework 在保存的同时要求SaveChanges提供一个ID? 实体框架 - Requiring an Id from SaveChanges, whilst saving? Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM