Linq to Entities插入,删除外键关系 [英] Linq to Entities Insert, Delete on Foreign key relation

查看:74
本文介绍了Linq to Entities插入,删除外键关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI



我有问题通过linq将表上的记录插入实体,其中两个表用外键连接。



我的第一张桌子是





 创建  [dbo]。[Emp](
[Id] [ int ] IDENTITY 1 1 NOT NULL
[EmpName] [ varchar ]( 50 NULL
CONSTRAINT [PK_Emp] PRIMARY KEY CLUSTERED

[Id] ASC
WITH (PAD _INDEX = OFF ,STATISTICS_NORECOMPUTE = OFF ,IGNORE_DUP_KEY = OFF ,ALLOW_ROW_LOCKS = ON ,ALLOW_PAGE_LOCKS = ON ON [ PRIMARY ]
ON [ PRIMARY ]





和第二张表是





 创建  [dbo ]。[EMpSecond](
[Id] [ int ] NOT NULL
[EmpAge] [ int ] NULL
ON [ PRIMARY ]

GO

ALTER TABLE [dbo]。 [EMpSecond] WITH CHECK ADD FOREIGN KEY ([Id])
REFERENCES [dbo]。[Emp]([Id])





我的插入C#代码是



 Emp emp =  new  Emp(); 
emp.EmpName = txtName.Text;
emp.Id = Convert.ToInt32(txtId.Text);

EMpSecond sec = new EMpSecond();
sec.EmpAge = Convert.ToInt32(txtAge.Text);
sec.Id = Convert.ToInt32(txtId.Text);
// sec.Emp.Id = emp.Id;

使用(TestEntities testEntities = new TestEntities())
{
testEntities .AddToEmps(EMP);
testEntities.SaveChanges();

testEntities.AddToEMpSeconds(sec);
testEntities.SaveChanges();
}





请给我解决方案,紧急

解决方案

< blockquote>

 Emp emp = new Emp(); 
emp.EmpName = txtName.Text;
emp.Id = Convert.ToInt32(txtId.Text); //这不应该是用户可输入的,ID是由DB

生成的EMpSecond sec = new EMpSecond();
sec.EmpAge = Convert.ToInt32(txtAge.Text);
sec.Id = Convert.ToInt32(txtId.Text);
//sec.Emp.Id = emp.Id;

使用(TestEntities testEntities = new TestEntities())
{
testEntities.AddToEmps(emp);
testEntities.SaveChanges();
//保存一旦发生,则在saveChanges()调用
sec.Id = emp.Id之后将填充emp.Id; //当emp添加到DB时,此行将sec.Id设置为新创建的ID。
testEntities.AddToEMpSeconds(sec);
testEntities.SaveChanges();
}


HI

I have problem to insert records on table through linq to entities, where two tables are connected with foreign key.

My first table is


CREATE TABLE [dbo].[Emp](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[EmpName] [varchar](50) NULL,
 CONSTRAINT [PK_Emp] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]



and second table is


CREATE TABLE [dbo].[EMpSecond](
	[Id] [int] NOT NULL,
	[EmpAge] [int] NULL
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[EMpSecond]  WITH CHECK ADD FOREIGN KEY([Id])
REFERENCES [dbo].[Emp] ([Id])



my insertion C# code is

Emp emp = new Emp();
            emp.EmpName = txtName.Text;
            emp.Id = Convert.ToInt32(txtId.Text);

            EMpSecond sec = new EMpSecond();
            sec.EmpAge = Convert.ToInt32(txtAge.Text);
            sec.Id = Convert.ToInt32(txtId.Text);
            //sec.Emp.Id = emp.Id;

            using (TestEntities testEntities = new TestEntities())
            {
                testEntities.AddToEmps(emp);
                testEntities.SaveChanges();

                testEntities.AddToEMpSeconds(sec);
                testEntities.SaveChanges();
            }



please give me solution, its urgent

解决方案

Emp emp = new Emp();
            emp.EmpName = txtName.Text;
            emp.Id = Convert.ToInt32(txtId.Text); // this should not be user enterable, ID's are generated by DB
 
            EMpSecond sec = new EMpSecond();
            sec.EmpAge = Convert.ToInt32(txtAge.Text);
            sec.Id = Convert.ToInt32(txtId.Text);
            //sec.Emp.Id = emp.Id;

            using (TestEntities testEntities = new TestEntities())
            {
                testEntities.AddToEmps(emp);
                testEntities.SaveChanges();
// once the save occurs, then the emp.Id will be populated after saveChanges() called
 sec.Id = emp.Id; // this line sets the sec.Id to the newly created ID when emp was added to DB.
                testEntities.AddToEMpSeconds(sec);
                testEntities.SaveChanges();
            }


这篇关于Linq to Entities插入,删除外键关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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