为什么将单个实体保存到我的 EF Core 数据库上下文会导致多次插入?
Why is saving a single entity to my EF Core database context resulting in multiple inserts?
我只是在构建一个模型,然后尝试通过 .SaveChanges()
函数将其插入数据库,但是当我 运行 SaveChanges()
发生密钥重复...
所以我深入研究了 SQL 日志记录,发现此模型插入正在尝试重新插入已经存在的数据...为什么会发生这种情况?
我的代码
using (var dbContext = new dbContext(_dbContextOptions))
{
Request request = new Request()
{
RequesterId = 1,
HelpRequestType = helpRequestTypeObject, //new model is being inserted for this on .SaveChanges()
Status = statusObject, //new model is being inserted for this on .SaveChanges()
AssignmentKeyId = 12,
TimeCreated = DateTime.Now,
CustomContactIds = null
};
//add request to transaction
dbContext.Request.Add(request);
dbContext.SaveChanges();
}
编辑:所以我更改了所有内容以仅使用 id 来创建模型作为外键引用(没有直接链接的实体),这似乎解决了我的问题。创建实体并应用 .SaveChanges() 后,模型将按预期链接到所有其他实体。看来我是因为使用了以前在不同 DbContext 下查询的实体而不是我用于创建这个新实体的活动实体而给自己造成了问题。
所以我不太确定为什么会出现这个问题,因为我认为我在其他地方看到过这个...但问题似乎是我直接将现有实体模型链接到新模型而不是通过 ID 链接它们...
我正在从数据库中查询现有实体并将它们直接链接到这个新的 Request 对象,而不仅仅是通过 ID...这导致 Entity Framework 试图重新制作所有现有实体...
我更改了使用模型而不是 ID 的地方,现在一切似乎都可以正常工作了。
But it appears the issue was that I was directly linking existing entity models to the new model instead of linking them by ID...
I was querying for existing entities from the database and linking them to this new Request object directly rather than just by ID... This resulted in Entity Framework attempting to remake all the existing entities...
当您使用来自多个 DbContext
的实体时,您需要小心。如果您从 contextA
查询一个 HelpRequestType
对象,并将其连接到属于 contextB
的对象,那么 contextB
没有跟踪 HelpRequestType
对象并且已经无法知道该对象已经是基础数据库的一部分。因此,它认为该对象是新的。如果两个上下文都是相同的派生类型,这甚至是正确的。
如果你因为某些原因需要使用两个不同的DbContext
对象,你需要先Attach()contextA
的HelpRequestType
对象到contextB
, 在 contextB
.
上调用 SaveChanges()
之前
通常,对于所有操作(工作单元模式)只使用一个 DbContext
对象要容易得多。您应该有充分的理由(性能、在多个数据存储上分区的模型等)使用多个 DbContext
.
另请参阅 Working with Disconnected Entity Graph in Entity Framework Core 了解更多详细信息。
我只是在构建一个模型,然后尝试通过 .SaveChanges()
函数将其插入数据库,但是当我 运行 SaveChanges()
发生密钥重复...
所以我深入研究了 SQL 日志记录,发现此模型插入正在尝试重新插入已经存在的数据...为什么会发生这种情况?
我的代码
using (var dbContext = new dbContext(_dbContextOptions))
{
Request request = new Request()
{
RequesterId = 1,
HelpRequestType = helpRequestTypeObject, //new model is being inserted for this on .SaveChanges()
Status = statusObject, //new model is being inserted for this on .SaveChanges()
AssignmentKeyId = 12,
TimeCreated = DateTime.Now,
CustomContactIds = null
};
//add request to transaction
dbContext.Request.Add(request);
dbContext.SaveChanges();
}
编辑:所以我更改了所有内容以仅使用 id 来创建模型作为外键引用(没有直接链接的实体),这似乎解决了我的问题。创建实体并应用 .SaveChanges() 后,模型将按预期链接到所有其他实体。看来我是因为使用了以前在不同 DbContext 下查询的实体而不是我用于创建这个新实体的活动实体而给自己造成了问题。
所以我不太确定为什么会出现这个问题,因为我认为我在其他地方看到过这个...但问题似乎是我直接将现有实体模型链接到新模型而不是通过 ID 链接它们...
我正在从数据库中查询现有实体并将它们直接链接到这个新的 Request 对象,而不仅仅是通过 ID...这导致 Entity Framework 试图重新制作所有现有实体...
我更改了使用模型而不是 ID 的地方,现在一切似乎都可以正常工作了。
But it appears the issue was that I was directly linking existing entity models to the new model instead of linking them by ID...
I was querying for existing entities from the database and linking them to this new Request object directly rather than just by ID... This resulted in Entity Framework attempting to remake all the existing entities...
当您使用来自多个 DbContext
的实体时,您需要小心。如果您从 contextA
查询一个 HelpRequestType
对象,并将其连接到属于 contextB
的对象,那么 contextB
没有跟踪 HelpRequestType
对象并且已经无法知道该对象已经是基础数据库的一部分。因此,它认为该对象是新的。如果两个上下文都是相同的派生类型,这甚至是正确的。
如果你因为某些原因需要使用两个不同的DbContext
对象,你需要先Attach()contextA
的HelpRequestType
对象到contextB
, 在 contextB
.
SaveChanges()
之前
通常,对于所有操作(工作单元模式)只使用一个 DbContext
对象要容易得多。您应该有充分的理由(性能、在多个数据存储上分区的模型等)使用多个 DbContext
.
另请参阅 Working with Disconnected Entity Graph in Entity Framework Core 了解更多详细信息。