简体   繁体   English

Linq-to-sql外键ID不会自动添加

[英]Linq-to-sql foreign key id not added automatically

I have a service-based file database. 我有一个基于服务的文件数据库。 With two table: Client and Visitation. 有两个表:Client和Visitation。 There is a One-to-Many relationship between tables: one client -> many visitations 表之间存在一对多关系:一个客户->许多访问

Tables: 表:

CREATE TABLE [dbo].[Clients] (
    [Id]          INT            IDENTITY (1, 1) NOT NULL,
    [Surname]     NVARCHAR (MAX) NULL,
    [Firstname]   NVARCHAR (MAX) NULL,
    [Birthday]    DATETIME       NULL,
    [Email]       NVARCHAR (MAX) NULL,
    [PhoneNumber] NVARCHAR (MAX) NULL,
    [Job]         NVARCHAR (MAX) NULL,
    [Note]        NVARCHAR (MAX) NULL,
    [Birthplace]  NVARCHAR (MAX) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

CREATE TABLE [dbo].[Visitationdb] (
    [Id]        INT      IDENTITY (1, 1) NOT NULL,
    [StartTime] DATETIME NULL,
    [ClientId]  INT      NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_Visitationdb_ToClients] FOREIGN KEY ([ClientId]) REFERENCES [dbo].[Clients] ([Id])
);

I created the required ClientId column and foreign key constraint. 我创建了必需的ClientId列和外键约束。

Linq-to-SQL LINQ到SQL

Added new Linq to SQL classes (dbml) file to the project and created the tables' classes with the help of the designer. 在项目中添加了新的Linq to SQL类(dbml)文件,并在设计器的帮助下创建了表的类。 (Here I have to modify one-to-one relationship to one-to-many) (在这里,我必须将一对一关系修改为一对多)

Problem 问题

DataContext MdpContext = new DataContext(Settings.Default.mdcdbConnectionString); //creating the context
var c = new Client //creating new client
    {
        Birthday = patientold.Birthday,
        Email = patientold.Email,
        Firstname = patientold.FirstName,
        Job = patientold.Job,
        PhoneNumber = patientold.PhoneNumber,
        Surname = patientold.SurName,
    };
clients.InsertOnSubmit(c);
MdpContext.SubmitChanges(); //now I will have my auto generated client id set by database       
var visit = new Visitationdb {StartTime = visitold.StartTime};
c.Visitationdb.Add(visit);
MdpContext.SubmitChanges();

EXCEPTION THROWN: Cannot insert the value NULL into column 'ClientId', table 'MDCDB.MDF.dbo.Visitationdb'; 异常抛出:无法将值NULL插入表'MDCDB.MDF.dbo.Visitationdb'的'ClientId'列中; column does not allow nulls. 列不允许为空。 INSERT fails. INSERT失败。

Questions 问题

Why L2S not handling inserting the ClientId automatically at line c.Visitationdb.Add(visit)? 为什么L2S不处理在c.Visitationdb.Add(visit)行自动插入ClientId的问题? Why do I have to SubmitChanges() every time I need an auto generated Id, is there any solution to handle all the new rows Ids and foreign keys by the framework after I added all clients and visitation? 为什么每次我需要一个自动生成的ID时都必须提交SubmitChanges(),在添加所有客户端和访问后,是否有解决方案来处理框架中的所有新行ID和外键?

Thank you for reading my question. 感谢您阅读我的问题。 Have a good coding! 编码好!

I have found the problem! 我发现了问题!

I thought that L2S handle the relationship (association) and correctly generate the entity after drag and drop into the designer. 我认为L2S处理关系(关联)并在拖放到设计器中后正确生成实体。

Problem In L2S instead of foreign key (ClientId) the Visitaitondb.Id was set in the Association. 问题在L2S中,在关联中设置了Visitaitondb.Id,而不是外键(ClientId)。

Solution Click on the Relation (Association) in the designer and at Properties / Participating Properties set the correct database fields. 解决方案在设计器中单击“关系(关联)”,然后在“属性/参与属性”处设置正确的数据库字段。 In my case: Client.Id -> Visitationdb.ClientId 就我而言:Client.Id-> Visitationdb.ClientId

Don't have to call SubmitChanges every time you need a generated Id or added a new entity. 不需要每次需要生成ID或添加新实体时都调用SubmitChanges。 At the end all ids will be set correctly by L2S! 最后,所有ID将由L2S正确设置!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM