2014-03-18 99 views
2

外鍵我有兩個表:operation和​​。兩列在實體框架

operation表具有複合主鍵operation_id: bigintdate_data: nvarchar(10)。​​也有這些列。基於這兩列的表格之間存在關係。添加ADO.NET實體數據模式後,出現兩個錯誤:

錯誤13101:參照約束的從屬角色中的所有屬性的類型必須與主體角色中的相應屬性類型相同。該類型的特性「operation_date_data」對實體「operation_category_element_relation」不引用約束「FK_operation_category_element_relation_operation」相匹配的屬性的類型「operation_id」對實體「操作」。

錯誤13101:類型在參照約束的從屬角色所有屬性必須是一樣的,在主體作用相應的屬性類型。該屬性類型的實體「operation_category_element_relation」 operation_id「在引用約束「FK_operation_category_element_relation_operation」不匹配的特性「date_data」類型的實體「操作」。

你能解釋一下問題是什麼以及如何擺脫它?

自動生成EDMX文件的內容是:

<?xml version="1.0" encoding="utf-8"?> 
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx"> 
    <!-- EF Runtime content --> 
    <edmx:Runtime> 
    <!-- SSDL content --> 
    <edmx:StorageModels> 
     <Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl"  Namespace="TEMPDataModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005"> 
     <EntityContainer Name="TEMPDataModelTargetContainer"></EntityContainer> 
     </Schema> 
    </edmx:StorageModels> 
    <!-- CSDL content --> 
    <edmx:ConceptualModels> 
     <Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="TEMPDataModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" annotation:UseStrongSpatialTypes="false"> 
     <EntityContainer Name="TEMPDataModelContainer" annotation:LazyLoadingEnabled="true"></EntityContainer> 
     </Schema> 
    </edmx:ConceptualModels> 
    <!-- C-S mapping content --> 
    <edmx:Mappings> 
     <Mapping xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs" Space="C-S"> 
     <Alias Key="Model" Value="TEMPDataModel" /> 
     <Alias Key="Target" Value="TEMPDataModel.Store" /> 
     <EntityContainerMapping CdmEntityContainer="TEMPDataModelContainer" StorageEntityContainer="TEMPDataModelTargetContainer"></EntityContainerMapping> 
     </Mapping> 
    </edmx:Mappings> 
    </edmx:Runtime> 
    <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) --> 
    <Designer xmlns="http://schemas.microsoft.com/ado/2009/11/edmx"> 
    <Connection> 
     <DesignerInfoPropertySet> 
     <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" /> 
     </DesignerInfoPropertySet> 
    </Connection> 
    <Options> 
     <DesignerInfoPropertySet> 
     <DesignerProperty Name="ValidateOnBuild" Value="true" /> 
     <DesignerProperty Name="EnablePluralization" Value="true" /> 
     <DesignerProperty Name="IncludeForeignKeysInModel" Value="true" /> 
     <DesignerProperty Name="UseLegacyProvider" Value="false" /> 
     <DesignerProperty Name="CodeGenerationStrategy" Value="None" /> 
     </DesignerInfoPropertySet> 
    </Options> 
    <!-- Diagram content (shape and connector positions) --> 
    <Diagrams></Diagrams> 
    </Designer> 
</edmx:Edmx> 
+1

哪些數據類型operation_id'和''operation_category_element_relation'的date_data''?他們需要是一樣的。 –

+0

他們都是一樣的 - 我沒有寫出來,但他們是相同的操作 – Marcin

回答

5

在這裏找到了答案:https://entityframework.codeplex.com/workitem/1735

發生這種情況的複合外鍵如果外鍵列的順序是從鍵列在表主要的順序不同。這可以用來攝製此示例表:

CREATE TABLE [dbo].[Table1] (
    [Id]  INT   NOT NULL, 
    [IdString] NVARCHAR (50) NOT NULL, 
    PRIMARY KEY CLUSTERED ([IdString] ASC, [Id] ASC) 
); 

CREATE TABLE [dbo].[Table3] 
(
    [TableId] INT NOT NULL PRIMARY KEY, 
    [IdString] NVARCHAR (50) NULL, 
    [Id]  INT   NULL, 
    CONSTRAINT [FK_Table3_ToTable] FOREIGN KEY (IdString, Id) REFERENCES [Table1](IdString, Id), 
) 

UPD。在我來說,我也不得不改變根據在PK場的順序領域的順序。

希望這有助於

+0

謝謝我有同樣的錯誤,但在我的情況下,我有一個問題,我使用3列的組合鍵,但表中的主鍵只指定了2個主鍵列(很難解釋,但很明顯EF沒有喜歡它) –

+0

謝謝!在我的情況下,我不得不在外表中將組合鍵的順序切換爲將外鍵設置爲2個複合鍵的第一個鍵 – sam

+1

我想指出(因爲我錯過了)它是_relative order_而不是必須匹配的列的_position_,以及它是table_中的_columns的順序,而不是外鍵中的選擇器的_order。例如,如果您明確指定列組合鍵所需的列順序,則需要在主表和從屬表中使用相同的列順序。在兩個表之間找到具有相同名稱的亂序列是可供選擇的,但是可能會錯過primary.Id應該與dependent.PrimaryId匹配的情況 – eisenpony

2

它看起來像你設置與翻轉列的關係。需要注意的是第一個錯誤指出的operation_date_data的類型不匹配operation_id。它應該與相關對象中的非id數據匹配。檢查關聯的參照約束以確保列的順序相同。

+0

correspondig列我剛剛檢查了SSMS的關係,只是要確定 - 列不翻轉。 – Marcin

+0

不,你需要在EDMX中檢查它。 –

+0

重點是edmx文件無法正確生成。我檢查了一些其他數據庫,確保VS沒有錯 - 一切都很好。對於操作和operation_category_element_relations表有問題的數據庫,edmx文件內容有點奇怪。我編輯了我的問題並將edmx文件的內容放在那裏。 – Marcin