c#-linq to sql-insert到同一表单的多个表中(在VIEW页面中) [英] c#-linq to sql-insert into multiple tables from same form (in VIEW page)

查看:66
本文介绍了c#-linq to sql-insert到同一表单的多个表中(在VIEW页面中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在视图中有一个表格收集学生注册详情。然后,为了问题,这个表单应该将其字段插入学生表和地址表,因为实际上它应该插入到其他表中。因此对于这个问题想象我有以下2个表:



学生:

-------

id

名称

姓氏

address_id



地址:

--------

id

house_name

house_address

locality

国家





如何使用linq对sql进行多次插入,以便用户从我的视图填写表格并提交,与学生表相关的详细信息进入其中,地址详细信息进入另一个表格?



谢谢......

Hi,

i have a form in a view that collects student registration details. This form then should insert its fields to the student table and the address table for the sake of the question, as in reality it should insert into other tables. Therefore for this question imagine that i have the following 2 tables:

Student:
-------
id
name
surname
address_id

Address:
--------
id
house_name
house_address
locality
country
etc

How can i do this multiple insert with linq to sql so that when a user fills up the form from my view and this is submitted, the details related to the student table go into it and the address details go into the other table?

Thanks......

推荐答案

为什么要使用LINQ?在任何情况下,答案必须是计算表之间的关系,并按照有意义的顺序插入数据。如果学生有地址ID,则需要先插入地址,以便分配该ID。如果学生只有一个地址,如果两个学生不能共享一个地址,那么这种设计就没有意义了。如果他们可以共享一个地址,你需要查询地址是否存在,然后创建它,如果它不存在。
Why do you have to use LINQ ? In any case, the answer has to be to work out the relationship between the tables, and insert the data in the order that makes sense. If a student has an address id, you need to insert the address first, so you have that id to assign. If a student only ever has one address, and if two students can''t share an address, this design makes no sense. If they CAN share an address, you need to look up if the address exists, then create it if it doesn''t.


这篇关于c#-linq to sql-insert到同一表单的多个表中(在VIEW页面中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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