插入多对多关系表
简单场景
[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 上进行? 或者说没关系?区别在哪里?
编辑
我问我是否应该制作
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
,条目也插入到City
> 和 ClientCity
。我应该怎样做才正确呢?
Simple scenario
[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);
Should I make InsertOnSubmit on every object(table) or only on clientCity?
Or it doesn't matter? Where's the difference?
EDIT
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();
or only
DatabaseDC dc = new DatabaseDC(connectionString);
dc.ClientCity.InsertOnSubmit(clientCity);//because this object has references to client and city
dc.SubmitChanges();
?
EDIT 2
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,您必须确保链接表在插入之前存在,否则可能会出现错误(取决于您是否限制了 sql 表)。
您还可以为插入和更新创建自定义存储过程调用,这些过程可以确保链接表的正确性。
恕我直言,linq-to-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.
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.)
从结果来看,确实没有什么关系。两种方式都是正确的。
第二个(只有一个 insertonsubmit)的优点
,第一个优点
正如你已经发现的,结果是一样的。
From the result, it really does not matter. Both ways are correct.
Advantages of the second (only one insertonsubmit)
Advantage of the first one
As you already found out, the result is the same.