1

我正在使用Visual Studio 2008,C#,LINQ to SQL,并使用datbase dbml GUI编辑器创建数据库。我想创建一个1对多的关系,但在我的情况下,1对象有2个主键。使用GUI edtior我创建的关联,但在创建程序运行和数据库的时候,我得到的错误:有多个外键的Linq-To-SQL关联问题

“被引用的表必须有主或候选键[FK名称= 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([email protected]"Screen_Pixel", [email protected]"_Screen", [email protected]"PKA1,PKA2", [email protected]"PKB1,PKB2", IsForeignKey=true)] 
[Association([email protected]"Screen_Pixel", [email protected]"_Pixels", [email protected]"PKB1,PKB2", [email protected]"PKA1,PKA2")] 

任何想法?

回答

0

为什么哟使用几个字段作为主字段(也称为“复合主字段”)?

一些学校和书籍教用“复合主键”,但在现实中,他们是不是一个好主意。许多软件不允许这些字段,即使这样做,也很难处理。

总是更好地使用单个字段主键。

我的建议是将这些字段保留为标准字段或外键,添加一个新字段,它是自动分配或增量的。

例子:

<Table Name="Screen"> <Column Name="ScreenKey" Type="System.Int64" IsPrimaryKey="true" CanBeNull="false" UpdateCheck="Never" /> 

<Column Name="ScreenField1" Type="System.Int64" IsPrimaryKey="false" CanBeNull="false" UpdateCheck="Never" /> 

<Column Name="ScreenField2" Type="System.Int32" IsPrimaryKey="false" CanBeNull="false" UpdateCheck="Never" /> 

<Association Name="Screen_Pixel" Member="Pixels" ThisKey="ScreenKey" OtherKey="ScreenKey" Type="Pixel" /> 

</Table> 

<Table Name="Pixel> 

<Column Name="PixelKey" Type="System.Int64" IsPrimaryKey="true" CanBeNull="false" UpdateCheck="Never" /> 

<Column Name="ScreenKey" Type="System.Int64" IsPrimaryKey="false" CanBeNull="false" UpdateCheck="Never" /> 

<Column Name="PixelField1" Type="System.Int64" IsPrimaryKey="false" CanBeNull="false" UpdateCheck="Never" /> 

<Column Name="PixelField2" Type="System.Int32" IsPrimaryKey="false" CanBeNull="false" UpdateCheck="Never" /> 

<Column Name="PixelField3" Type="System.Int32" IsPrimaryKey="false" CanBeNull="false" UpdateCheck="Never" /> 

<Association Name="Screen_Pixel" Member="Screen" ThisKey="ScreenKey" OtherKey="ScreenKey" Type="Screen" IsForeignKey="true" /> 

</Table> 
+0

我是不是该数据库的原作者,这是数据库是怎么写的。我曾希望没有这样做,因为它更简单,更直观,让多个外键定义的对象。如果星期一我还没有听到任何更多的回复,我会尽力回应。 – John 2011-01-07 22:35:19

1

因为我已经和类似的东西挣扎,这个问题就接近我的,我会在这里为我的2美分的下一个灵魂可能与同样的问题来通过(从对象模型在运行时创建数据库)。

如果你的组合键并没有真正成为一个主键(即你只是想提供组合的独特性)的解决方案是:

  • 添加一个正主键(整数身份分贝生成的列),您将从外键引用。
  • 提供唯一性约束与您的DataContext的一个额外的步骤,例如:

    yourDataContext.ExecuteCommand("ALTER TABLE Stuff ADD UNIQUE (unique_thing_1, unique_thing_2, unique_thihng_3)"); 
    

事后提交尝试插入重复字段时会抛出异常。