[英]How to insert data into 4 tables that have relationships using LINQ to sql
I have 4 tables in SQL server. 我在SQL Server中有4个表。 These tables have relationships with foreign keys.
这些表与外键有关系。 i get 20 fields from user in asp.net MVC website and want to insert these 20 fields(Name,Family,Address,Account Number,...) to these tables.
我在asp.net MVC网站上从用户那里获得20个字段,并希望将这20个字段(名称,家庭,地址,帐号,...)插入到这些表中。 For this operation i use LINQ to SQL in c#.
对于此操作,我在C#中使用LINQ to SQL。 I want the operation of inserting to be in a transaction scope.
我希望插入操作在事务范围内。
person table: [Row Id],[Name],[family],[Father Name],[Sex],[Birth Date],[Nationality Code],[Issue Number],[Marital Status],[live] 人物表:[行号],[姓名],[家庭],[父亲姓名],[性别],[出生日期],[国籍代码],[问题编号],[婚姻状况],[居住]
Address table: [Row Id],[Person_Row Id],[State City_Row Id],[Address],[Tel],[Mobile] 地址表:[行ID],[人_行ID],[州城市_行ID],[地址],[电话],[移动电话]
Account Table: [Row Id],[Account Number],[Card Number] 帐户表:[行号],[帐户号],[卡号]
Patient File Table: [Row Id],[File Code],[Person_Row Id],[Doctor_Row Id],[Charity_Row Id] ,[Illness_Row Id],[Account_Row Id] 患者档案表:[行ID],[文件代码],[人_行ID],[医生_行ID],[慈善_行ID],[疾病_行ID],[帐户_行ID]
This can do what you want : 这可以做你想要的:
using(YourDbContext dc = new YourDbContext())
{
person prs = new person();
prs.Name = "person1";
prs.FatherName = "Father1";
...
prs.Addresses.Add(
new Address()
{
Address = "Address1 No1",
Tel = "000000",
...
});
prs.Accounts.Add(
new Account()
{
AccountNumber = "123456",
CardNumber = "000000"
....
});
dc.Persons.Add(prs);
dc.SaveChanges();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.