linq 到实体保存

发布于 2024-10-20 02:50:27 字数 1409 浏览 6 评论 0原文

我有以下数据库结构。

在此处输入图像描述

我已将 Buyer 保存到数据库中。我有买家 ID。我必须在数据库中保存买家的联系信息,因为我在数据库中保存联系人信息,所以我还需要在 buyercontact 表中保存 buyercontact 。当我尝试在 edmx 文件中添加 BuyerContact 表时,Visual Studio 2010 不允许我这样做。

如何在 buyercontact 表中保存信息?请帮忙。

他们说,如果在数据库/edmx 中保持适当的关系,当我放弃“联系人”时,buyercontact 也会保存到数据库中。我通过互联网搜索,我也发现了类似的问题,但我无法理解。

请帮忙。我是否必须编写一些特殊的代码来保存 BuyerContact

当我编写此代码来保存联系人以及 buyercontact 时,出现错误

无法插入重复的键 dbo.买家

Contact objContact = new Contact();
objContact.FirstName = firstName;
objContact.MiddleName = middleName;
objContact.Lastname = lastName;
objContact.CreatedDate = DateTime.Now;
objContact.AddressId = gAddressId;                    

var buyer = _buyerRepository.GetSingle(x => x.BuyerId == BuyerID);
objContact.Buyer = buyer;

_contactRepository.Add(objContact);
_contactRepository.Save()

如果我删除 objContact.Buyer = Buyer;,则联系信息会成功保存,但 buyercontact 不会保存。现在我的问题是,一旦我与买家建立了联系,我还需要将其保存在 BuyerContact 表中。

我已经在此处浏览过,但O无法理解。我使用通用 Linq-to-SQL 存储库来执行常见数据库操作(例如 CRUD)。

请帮忙,谢谢

I have a following database structure.

enter image description here

I have already saved Buyer in database. I have buyer id. I have to save contact information of a buyer in the database, as I save contact in the database I also need to save buyercontact in buyercontact table. When I try to add BuyerContact table in the edmx file, Visual Studio 2010 won't allow me to do that.

How do I save information in buyercontact table? Please help.

They say if appropriate relationship is maintained in the database/edmx, buyercontact will be saved to database as well when i cave "contact". I search through the internet, i also found similar questions asked but i could not understand.

Please help. do i have to write some special code to save BuyerContact

When I write this code to save contact along with buyercontact, I get error

Cannot insert duplicate key in
dbo.Buyer

Contact objContact = new Contact();
objContact.FirstName = firstName;
objContact.MiddleName = middleName;
objContact.Lastname = lastName;
objContact.CreatedDate = DateTime.Now;
objContact.AddressId = gAddressId;                    

var buyer = _buyerRepository.GetSingle(x => x.BuyerId == BuyerID);
objContact.Buyer = buyer;

_contactRepository.Add(objContact);
_contactRepository.Save()

If I remove objContact.Buyer = buyer;, then contact information is saved successfully but buyercontact is not saved. Now my problem is as soon as I have contact associated with the buyer I also need to save it in the BuyerContact table.

I have already been through post here, but O could not understand. I have used generic Linq-to-SQL repository for common database operations like CRUD.

Please help, thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

月下凄凉 2024-10-27 02:50:27

尝试这个,而不是:

objContact.Buyer = buyer;
_contactRepository.Add(objContact);
_contactRepository.Save()

替换为:

buyer.Contacts.Add(objContact);
_buyerRepository.Save();

还假设您在工作单元的所有存储库中使用相同的上下文对象

try this, instead of:

objContact.Buyer = buyer;
_contactRepository.Add(objContact);
_contactRepository.Save()

replace with:

buyer.Contacts.Add(objContact);
_buyerRepository.Save();

also assuming you are using same context object across all repositories for unit of work

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