实体框架中的两列外键

Mar*_*cin 4 .net c# sql entity-framework visual-studio-2013

我有两张桌子:operationoperation_category_element_relation.

operation表有一个复合主键operation_id: bigintdate_data: nvarchar(10).在operation_category_element_relation具有这些列也是如此.基于这两列的表之间存在关系.添加ADO.NET实体数据模式后,我收到两个错误:

错误13101:引用约束的从属角色中的所有属性的类型必须与主体角色中的相应属性类型相同.实体'operation_category_element_relation'上的属性'operation_date_data'的类型与引用约束'FK_operation_category_element_relation_operation'中实体'operation'上的属性'operation_id'的类型不匹配.

错误13101:引用约束的从属角色中的所有属性的类型必须与主体角色中的相应属性类型相同.实体'operation_category_element_relation'上的属性'operation_id'的类型与引用约束'FK_operation_category_element_relation_operation'中实体'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>
Run Code Online (Sandbox Code Playgroud)

nZe*_*eus 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),
)
Run Code Online (Sandbox Code Playgroud)

UPD.在我的情况下,我还必须根据 PK中的字段顺序更改字段的顺序.

希望这可以帮助

  • 我想指出(因为我错过了它)必须匹配的是列的_relative order_而不是_position_,并且重要的是table_中的_columns的顺序而不是列中的selectors_的_order外键。例如,如果您根据复合键的要求显式分配列顺序,则需要在主表和从属表中使用相同的列顺序。可以找到两个表之间具有相同名称的无序列,但可能会遗漏primary.Id应该匹配dependent.PrimaryId的情况 (2认同)