实体框架跨多个上下文插入多对多关系
我有一个 Events 表和一个 PaymentTypes 表,它们通过 Events_PaymentTypes 连接表链接。 EF 允许我通过执行 Event.PaymentTypes.Add(PaymentType) 将条目添加到联接表中。但是,我从一个上下文中获取 PaymentTypes 并在另一个上下文中执行添加操作。如果我先分离 PaymentType,它会在 PaymentTypes 表中创建一条新记录,这不是我想要的。我该如何解决这个问题?
I have an Events table and a PaymentTypes table which are linked by Events_PaymentTypes join table. EF allows me to add an entry into the join table by doing Event.PaymentTypes.Add(PaymentType). However, I'm getting the PaymentTypes from one context and doing the add operation in another context. If I detach the PaymentType first, it creates a new record in the PaymentTypes table, which is not what I want. How can I get around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想通了。在添加它之前我没有将它附加到其他上下文。
figured it out. I wasn't attaching it to the other context before adding it.