我正在使用 Visual Studio 2008、C#、LINQ to SQL,并使用 datbase dbml GUI 编辑器来创建数据库。我想创建一个一对多的关系,但在我的例子中,1 个对象有 2 个主键。使用 GUI edtior 我创建了关联,但是当程序运行并创建数据库时,我收到错误:
“引用的表必须有一个主键或候选键。[FK Name = Screen_Pixel]”
在 dbml 中创建的 XML 如下所示:
<Table Name="Screen">
<Column Name="PKA1" Type="System.Int64" IsPrimaryKey="true" CanBeNull="false" UpdateCheck="Never" />
<Column Name="PKA2" Type="System.Int32" IsPrimaryKey="true" CanBeNull="false" UpdateCheck="Never" />
<Association Name="Screen_Pixel" Member="Pixels" ThisKey="PKA1,PKA2" OtherKey="PKB1,PKB2" Type="Pixel" />
</Table>
<Table Name="Pixel>
<Column Name="PKB1" Type="System.Int64" IsPrimaryKey="true" CanBeNull="false" UpdateCheck="Never" />
<Column Name="PKB2" Type="System.Int32" IsPrimaryKey="true" CanBeNull="false" UpdateCheck="Never" />
<Column Name="PKB3" Type="System.Int32" IsPrimaryKey="true" CanBeNull="false" UpdateCheck="Never" />
<Association Name="Screen_Pixel" Member="Screen" ThisKey="PKB1,PKB2" OtherKey="PKA1,PKA2" Type="Screen" IsForeignKey="true" />
</Table>
在 C# 代码中生成的关联是:
[Association(Name=@"Screen_Pixel", Storage=@"_Screen", ThisKey=@"PKA1,PKA2", OtherKey=@"PKB1,PKB2", IsForeignKey=true)]
[Association(Name=@"Screen_Pixel", Storage=@"_Pixels", ThisKey=@"PKB1,PKB2", OtherKey=@"PKA1,PKA2")]
有任何想法吗?