我在尝试让继承在EF4中工作时遇到了很多麻烦。原始模型非常大,但我已经设法在只有两个实体的模型中复制它。
基本上,这两个表是“资产”和“问卷调查”。问卷继承资产(即问卷是资产)。他们通过问卷调查来实现这一点,其中AssetID链接回资产。从技术上讲,设计可能不是最好的,但是我试图让这个10年历史的数据库工作而没有任何重大的重新设计......
因此,表格是:
资产
Content_Questionnaire
这个模型非常简单 - 基本上是:
EDMX文件是:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="SnapServerModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
<EntityContainer Name="SnapServerModelStoreContainer">
<EntitySet Name="Asset" EntityType="SnapServerModel.Store.Asset" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Content_Questionnaire" EntityType="SnapServerModel.Store.Content_Questionnaire" store:Type="Tables" Schema="dbo" />
<AssociationSet Name="FK_Content_Questionnaire_Asset" Association="SnapServerModel.Store.FK_Content_Questionnaire_Asset">
<End Role="Asset" EntitySet="Asset" />
<End Role="Content_Questionnaire" EntitySet="Content_Questionnaire" />
</AssociationSet>
</EntityContainer>
<EntityType Name="Asset">
<Key>
<PropertyRef Name="AssetID" />
</Key>
<Property Name="AssetID" Type="uniqueidentifier" Nullable="false" />
</EntityType>
<EntityType Name="Content_Questionnaire">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="uniqueidentifier" Nullable="false" />
<Property Name="AssetID" Type="uniqueidentifier" Nullable="false" />
</EntityType>
<Association Name="FK_Content_Questionnaire_Asset">
<End Role="Asset" Type="SnapServerModel.Store.Asset" Multiplicity="1" />
<End Role="Content_Questionnaire" Type="SnapServerModel.Store.Content_Questionnaire" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Asset">
<PropertyRef Name="AssetID" />
</Principal>
<Dependent Role="Content_Questionnaire">
<PropertyRef Name="AssetID" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema></edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="SnapServerModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="SnapServerEntities" annotation:LazyLoadingEnabled="true">
<EntitySet Name="Assets" EntityType="SnapServerModel.Asset" />
<AssociationSet Name="FK_Content_Questionnaire_Asset" Association="SnapServerModel.FK_Content_Questionnaire_Asset">
<End Role="Asset" EntitySet="Assets" />
<End Role="Content_Questionnaire" EntitySet="Assets" />
</AssociationSet>
</EntityContainer>
<EntityType Name="Asset">
<Key>
<PropertyRef Name="AssetID" />
</Key>
<Property Type="Guid" Name="AssetID" Nullable="false" />
<NavigationProperty Name="Content_Questionnaire" Relationship="SnapServerModel.FK_Content_Questionnaire_Asset" FromRole="Asset" ToRole="Content_Questionnaire" />
</EntityType>
<EntityType Name="Content_Questionnaire" BaseType="SnapServerModel.Asset">
<Property Type="Guid" Name="Id" Nullable="false" />
<NavigationProperty Name="Asset" Relationship="SnapServerModel.FK_Content_Questionnaire_Asset" FromRole="Content_Questionnaire" ToRole="Asset" />
</EntityType>
<Association Name="FK_Content_Questionnaire_Asset">
<End Type="SnapServerModel.Asset" Role="Asset" Multiplicity="1" />
<End Type="SnapServerModel.Content_Questionnaire" Role="Content_Questionnaire" Multiplicity="1" />
<ReferentialConstraint>
<Principal Role="Asset">
<PropertyRef Name="AssetID" />
</Principal>
<Dependent Role="Content_Questionnaire">
<PropertyRef Name="AssetID" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs">
<EntityContainerMapping StorageEntityContainer="SnapServerModelStoreContainer" CdmEntityContainer="SnapServerEntities">
<EntitySetMapping Name="Assets">
<EntityTypeMapping TypeName="IsTypeOf(SnapServerModel.Asset)">
<MappingFragment StoreEntitySet="Asset">
<ScalarProperty Name="AssetID" ColumnName="AssetID" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(SnapServerModel.Content_Questionnaire)">
<MappingFragment StoreEntitySet="Content_Questionnaire">
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="AssetID" ColumnName="AssetID" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<Designer xmlns="http://schemas.microsoft.com/ado/2008/10/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" />
</DesignerInfoPropertySet>
</Options>
<!-- Diagram content (shape and connector positions) -->
<Diagrams>
<Diagram Name="Model1">
<EntityTypeShape EntityType="SnapServerModel.Asset" Width="1.5" PointX="0.75" PointY="2.125" Height="1.4033821614583335" />
<EntityTypeShape EntityType="SnapServerModel.Content_Questionnaire" Width="1.5" PointX="3" PointY="2.125" Height="1.4033821614583339" />
<AssociationConnector Association="SnapServerModel.FK_Content_Questionnaire_Asset" >
<ConnectorPoint PointX="2.25" PointY="2.4133455403645834" />
<ConnectorPoint PointX="3" PointY="2.4133455403645834" />
</AssociationConnector>
<InheritanceConnector EntityType="SnapServerModel.Content_Questionnaire" ManuallyRouted="false" >
<ConnectorPoint PointX="2.25" PointY="2.8266910807291667" />
<ConnectorPoint PointX="3" PointY="2.8266910807291667" />
</InheritanceConnector>
</Diagram>
</Diagrams>
</Designer>
</edmx:Edmx>
现在我的问题是无论我尝试过什么 - 我收到两条错误消息:
我究竟如何摆脱这些错误?
这可能是我遗漏的一件非常明显的事情 - 但 ANY 帮助甚至是正确方向的推动都会非常感激。我今天一直在努力解决这个问题!
提前感谢您提供的任何帮助。
答案 0 :(得分:2)
好的,您需要在模型中修复一些内容:
1.首先,您需要将Content_Questionnaire表上的主键更改为AssetID,据我所知,只有当关联位于两端的主键上时,EF才支持2实体之间的继承。所以看起来应该是这样的:
<EntityType Name="Content_Questionnaire">
<Key>
<!--First Change:-->
<PropertyRef Name="AssetID" />
</Key>
<Property Name="Id" Type="uniqueidentifier" Nullable="false" />
<Property Name="AssetID" Type="uniqueidentifier" Nullable="false" />
</EntityType>
2.派生实体的多重性必须为0..1(现在为*)
<Association Name="FK_Content_Questionnaire_Asset">
<End Role="Asset" Type="SnapServerModel.Store.Asset" Multiplicity="1" />
<!--Secound Change:-->
<End Role="Content_Questionnaire"
Type="SnapServerModel.Store.Content_Questionnaire" Multiplicity="0..1" />
<ReferentialConstraint>
<Principal Role="Asset">
<PropertyRef Name="AssetID" />
</Principal>
<Dependent Role="Content_Questionnaire">
<PropertyRef Name="AssetID" />
</Dependent>
</ReferentialConstraint>
</Association>
3.此外,在您的模型表面上,只需选择两个实体之间的关联线并将其删除(您的导航属性将消失),通过继承,您不需要它。
现在,再次尝试构建项目,您将看到编译错误消失。