简体   繁体   English

LINQ to SQL并为单个主键向外部表添加多个记录

[英]LINQ to SQL and adding multiple records to foreign table for a single primary key

I have a database with around 20 tables, and one of them is an "attachment" table. 我有一个大约有20个表的数据库,其中一个是“附件”表。 All but one table has a foreign key to the "Identifier" table so that each record in the Identifier table identifies one record in all of the other tables. 除一个表外,所有表都具有“标识符”表的外键,因此,标识符表中的每条记录都可以标识所有其他表中的一条记录。

For the attachments table, I have my two primary keys which identify the record from the Identifiers table that it is associated with. 对于附件表,我有两个主键,它们从“标识符”表中标识与其关联的记录。 I have two other fields, one of which is supposed to act as an "index" to indicate that there can be multiple attachments per Identifier. 我还有另外两个字段,其中一个应该用作“索引”,以指示每个标识符可以有多个附件。

The issue I'm running into is that I cannot figure out how to add multiple attachments to a single Identifier object using LINQ to SQL. 我遇到的问题是我无法弄清楚如何使用LINQ to SQL将多个附件添加到单个Identifier对象。 I know I cannot have multiple records with the same primary keys, so would I have to make the attachment index a primary key as well? 我知道我不能有多个具有相同主键的记录,因此我是否也必须使附件索引成为主键? Would that turn the Attachment member of the Identifier object in my C# project into a list? 这会将C#项目中Identifier对象的Attachment成员转换为列表吗? I guess I'm not understanding how I can have multiple records in the Attachment table refer to the same Identifier record. 我想我不明白如何在附件表中有多个记录引用同一标识符记录。

将附件表中的索引列更改为主键并刷新Visual Studio中的DBML架构后,引用Identifier类中的Attachment实体的EntityRef对象更改为EntitySet,从而允许我向每个Identifier添加多个Attachment实体。

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

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