linq 到实体保存
我有以下数据库结构。
我已将 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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这个,而不是:
替换为:
还假设您在工作单元的所有存储库中使用相同的上下文对象
try this, instead of:
replace with:
also assuming you are using same context object across all repositories for unit of work