插入多对多关系表

发布于 2024-12-14 08:54:30 字数 1016 浏览 0 评论 0原文

简单场景

[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 技术交流群。

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

发布评论

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

评论(2

倾城泪 2024-12-21 08:54:30

通常,您必须确保链接表在插入之前存在,否则可能会出现错误(取决于您是否限制了 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.)

爱要勇敢去追 2024-12-21 08:54:30

从结果来看,确实没有什么关系。两种方式都是正确的。

第二个(只有一个 insertonsubmit)的优点

  • 干净
  • 是代码更

,第一个优点

  • 对于 Linq2sql 新手来说更容易阅读/理解
  • ,如果没有正确的引用,则会完成插入(或者这是一个缺点?,由你决定)

正如你已经发现的,结果是一样的。

From the result, it really does not matter. Both ways are correct.

Advantages of the second (only one insertonsubmit)

  • cleaner
  • less code

Advantage of the first one

  • for people who are new to Linq2sql easier to read/understand
  • also if there are no correct references, an insert will be done (or is this a disadvantage?, up to you)

As you already found out, the result is the same.

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