gpt4 book ai didi

c# - Entity Framework 中的两列外键

转载 作者:太空狗 更新时间:2023-10-30 00:41:10 25 4
gpt4 key购买 nike

我有两个表:operationoperation_category_element_relation

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

Error 13101: The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'operation_date_data' on entity 'operation_category_element_relation' does not match the type of property 'operation_id' on entity 'operation' in the referential constraint 'FK_operation_category_element_relation_operation'.

Error 13101: The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'operation_id' on entity 'operation_category_element_relation' does not match the type of property 'date_data' on entity 'operation' in the referential constraint 'FK_operation_category_element_relation_operation'.

您能解释一下问题是什么以及如何解决吗?

自动生成的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),
)

更新。在我的例子中,我还必须根据 PK 中的字段顺序更改字段顺序。

希望对你有帮助

关于c# - Entity Framework 中的两列外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22486978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com