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)"); 
    

事後提交嘗試插入重複字段時會拋出異常。