繁体   English   中英

实体框架中的两列外键

[英]Two column foreign key in entity framework

我有两个表: 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>

在这里找到答案: 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中的字段顺序更改字段的顺序。

希望这可以帮助

看起来你已经设置了与翻转列的关系。 需要注意的是第一个错误指出的类型operation_date_data不匹配operation_id 它应该与相关对象中的非数据ID匹配。 检查关联上的引用约束以确保列的顺序相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM