插入到许多一对多的关系表 [英] Insert into many-to-many relationship tables

查看:161
本文介绍了插入到许多一对多的关系表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的场景

[ClientTable]: ClientId, ClientName, Phone, Age
[CityTable]: CityID, CityName, Country
[ClientCityTable]: ClientCityID, ClientID, CityID

Client client = new Client("John", 123456789, 40);
City city = new City("NY", USA);
ClientCity clientCity = new ClientCity(client, city);



我应该InsertOnSubmit中的每个对象(表)或仅在clientCity?
或不要紧?哪里不同?​​

Should I make InsertOnSubmit on every object(table) or only on clientCity? Or it doesn't matter? Where's the difference?

修改

I'ms问我是否应该

I'ms asking if should I make

DatabaseDC dc = new DatabaseDC(connectionString);
dc.Client.InsertOnSubmit(client);
dc.City.InsertOnSubmit(city);
dc.ClientCity.InsertOnSubmit(clientCity);
dc.SubmitChanges();

或只

DatabaseDC dc = new DatabaseDC(connectionString);
dc.ClientCity.InsertOnSubmit(clientCity);//because this object has references to client and city
dc.SubmitChanges();



?

修改2

我做了一些尝试,甚至我用 InsertOnSubmit 仅在客户端,条目也插入城市 ClientCity 。我应该怎样做是正确的?

I made a few attempts and even of I use InsertOnSubmit only on client, entries are inserted also to City and ClientCity. How should I do it correctly?

推荐答案

通常情况下,你必须确保该链接的表插入之前存在,或者,你可能会得到一个错误(如果你已经制约你的SQL表依赖)。

Typically you have to insure that the linked tables exist before the insert or you may get an error (depends if you have constrained your sql tables).

您还可以创建自定义存储过程的插入和更新要求,这些程序可以保证链接表是正确的

You can also create custom stored procedure calls for the inserts and updates, these procedures could insure the linked tables are correct.

恕我直言LINQ到SQL是好的制作复杂的选择,但不是那么容易用于更新数据库。 (通常,我见过它造成严重的性能瓶颈。)

IMHO linq-to-sql is good for making complicated selections but not so easy to use for updating the database. (Often I've seen it create serious performance bottlenecks.)

这篇关于插入到许多一对多的关系表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆