LINQ中的一对一关系 [英] One to One relationships in LINQ

查看:88
本文介绍了LINQ中的一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我对LINQ和1对1的关系有疑问.基本上我有2个表,这些表形成1对1的关系.一个表的PK(表A)是自动生成的编号,其他表PK也是FK(表B).

现在,当我创建表A的实体的新实例并将表B的新实体附加到A并尝试将更改提交到数据库时,我得到了"INSERT语句与FOREIGN KEY约束冲突"例外.从数据上下文的日志中可以推断出,外键不会像我认为的那样自动更新.

有什么想法吗?

如果需要进一步说明,请告诉我.

在此先感谢

Hi All,

I have an issue with LINQ and 1 to 1 relationships. Basically I have 2 tables that form a 1 to 1 relationship. The one table''s PK (table A) is an auto generated number and the other tables PK is also the FK (table B).

Now when I create a new instance of table A''s entity and attach a new entity of table B to A and try to commit the changes to the database, i get a ''The INSERT statement conflicted with the FOREIGN KEY constraint'' exception. from what I can deduce from the data context''s log, the foreign key is not updated automatically as I would think it should.

Any ideas?

Please let me know if I need to be more clear in my question.

thanks in advance

推荐答案

只要在数据库级别定义了关系(一对一),它就可以工作.

我刚刚进行了测试,希望对您有所帮助

1.我有表Employee&地址-在SQL Server的数据库图"节点中定义并验证一对一关系.

As far as you have the relationship (one to one) defined at the database level then it will work.

I just did a test and hope it helps

1.I have tables Employee & Address - the one to one relationship is defined and verified in the "database diagrams" node of SQL server.

CREATE TABLE [dbo].[employees](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL,
 CONSTRAINT [PK_employees] 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]





CREATE TABLE [dbo].[addresses](
    [AddressID] [int] IDENTITY(1,1) NOT NULL,
    [EmpID] [int] NOT NULL,
    [Address] [varchar](50) NULL,
 CONSTRAINT [PK_addresses] PRIMARY KEY CLUSTERED
(
    [AddressID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]




前端的以下代码有效




The following code at the front end works

try {
              DataClasses1DataContext db = new DataClasses1DataContext();

              employee emp = new employee();
              emp.Name = "Bala";
              db.employees.InsertOnSubmit(emp);

              address add = new address();
              add.Address1 = "UK";
              add.employee = emp;
              db.addresses.InsertOnSubmit(add);

              db.SubmitChanges();
              MessageBox.Show("OK");

          } catch (Exception ex) {
              MessageBox.Show (ex.Message);
          }


为这两个表创建两个不同的对象
首先提交第一个表(objTableA)
然后将此自动生成的PK ID设置为第二个表对象的FK
objTableB.FKField = objTableA.PKField
然后提交第二张表.

这背后的原因是,在tableB中,您的设置应首先存在于tableA中的FK ID,因为您具有该关系.

希望对您有帮助.

〜Amol.
create two different objects for those two tables
First commit first table (objTableA)
then set this auto generated PK id to the FK of second table object
objTableB.FKField = objTableA.PKField
and then commit second table.

reason behind this is that in tableB the FK id that your setting should exists in the tableA first, as you have the relation.

Hope this will help you.

~Amol.


这篇关于LINQ中的一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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