需要向数据库添加三个具有两个 1-* 关系的实体
我需要调用 SaveChanges() 一次 这是我的模型(简化):
|Publisher| 1 -------- * |书籍| * --------1 |地点|
这就是我正在做的:
void AddBook(Publisher pub, Location loc) {
Book b = Book.CreateBook(0,...);
context.AddRelatedObject(pub, "Books", b);
context.AddToLocations(loc);
context.AddLink(loc, "Books", b);
context.SaveChanges();
}
这会引发异常:
“Context.Books”中的实体参与“LocationBook”关系。找到 0 个相关“位置”。 1 需要“位置”
这里的上下文是 WCF 数据上下文(使用 Web 服务引用)
在调试器下,我可以看到图书和位置的实体以及位置 -> 图书的链接
如果您能告诉我如何执行此操作,请感激不尽?
I need to call SaveChanges() once
Here is my model (simplified):
|Publisher| 1 ------- * |Book| * --------1 |Location|
Here is what I'm doing:
void AddBook(Publisher pub, Location loc) {
Book b = Book.CreateBook(0,...);
context.AddRelatedObject(pub, "Books", b);
context.AddToLocations(loc);
context.AddLink(loc, "Books", b);
context.SaveChanges();
}
this throws exception:
Entities in 'Context.Books' participate in the 'LocationBook' relationship. 0 related 'Location' were found. 1 'Location' is expected
The context here is WCF Data context (using web service reference)
Under debugger i can see Entities for Book and Location and Link for Location->Book
Appreciate if you tell me how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 context.SaveChanges(SaveChangesOption.Batch)。
如果没有它,它会将每个更新作为单独的请求发送,并且服务器将尝试将每个请求单独应用于数据库。如果存在外键约束,则会失败。
使用 Batch,客户端将在一个请求中发送所有内容,服务器将立即应用所有内容,这应该可行。
Use context.SaveChanges(SaveChangesOption.Batch).
Without that it will send each update as a separate request and the server will try to apply each of those requests separately to the DB. If there are foreign key constrains this will fail.
With Batch the client will send everything in one request and server will apply it all at once, which should work.