EntityFramework模型首先,来自外键的复合主键

时间:2012-11-23 14:27:13

标签: entity-framework entity-framework-5 ef-model-first

我有一个Entity Framework 5 Model First项目,包含以下表格:

Extension
  Id : Int32, PK
  Number: String

User
  Id: Int32, PK
  Name: String

UserExtensionMap
  UserId : Int32 (FK -> User.Id), PK
  ExtensionId : Int32 (FK -> Extension.Id), PK

将上述表和关联设置为UserExtensionMap时,我得到UserId和ExtensionId未映射的错误。

所以我右键单击UserExtensionMap表并选择表映射并获取以下屏幕 mapping

我可以设置Users.Id to UserExtensionUser.IdExtensions.Id to UserExtension.ExtensionId的地图,但没有任何地方可以将Extensions.Number和User.Username映射到 - 我很困惑!

我在尝试编译时遇到错误:

Error 3024: Problem in mapping fragments starting at line 119:Must specify mapping for all key properties (UserExtensionMaps.ExtensionId, UserExtensionMaps.UserId) of the EntitySet UserExtensionMaps.

当我删除UserExtensionMap的上述地图时,我收到错误:

Error 3027: No mapping specified for the following EntitySet/AssociationSet - UserExtensionMaps.

我的模型XML文件如下:

<?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 Namespace="Server.Store" Alias="Self" Provider="System.Data.SqlServerCe.4.0" ProviderManifestToken="4.0" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
  <EntityContainer Name="ServerStoreContainer">
    <EntitySet Name="Users" EntityType="Server.Store.Users" store:Type="Tables" Schema="dbo" />
    <EntitySet Name="Extensions" EntityType="Server.Store.Extensions" store:Type="Tables" Schema="dbo" />
  </EntityContainer>
  <EntityType Name="Users">
    <Key>
      <PropertyRef Name="Id" />
    </Key>
    <Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
    <Property Name="Username" Type="nvarchar" Nullable="false" />
  </EntityType>
  <EntityType Name="Extensions">
    <Key>
      <PropertyRef Name="Id" />
    </Key>
    <Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
    <Property Name="Number" Type="nvarchar" Nullable="false" />
  </EntityType>
</Schema></edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema Namespace="Server" Alias="Self" p1:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:p1="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
        <EntityContainer Name="ServerEntities" p1:LazyLoadingEnabled="true" >
          <EntitySet Name="Users" EntityType="Server.User" />
          <EntitySet Name="Extensions" EntityType="Server.Extension" />
          <EntitySet Name="UserExtensionMaps" EntityType="Server.UserExtensionMap" />
          <AssociationSet Name="ExtensionUserExtensionMap" Association="Server.ExtensionUserExtensionMap">
            <End Role="Extension" EntitySet="Extensions" />
            <End Role="UserExtensionMap" EntitySet="UserExtensionMaps" />
          </AssociationSet>
          <AssociationSet Name="UserUserExtensionMap" Association="Server.UserUserExtensionMap">
            <End Role="User" EntitySet="Users" />
            <End Role="UserExtensionMap" EntitySet="UserExtensionMaps" />
          </AssociationSet>
        </EntityContainer>
        <EntityType Name="User">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
          <Property Type="String" Name="Username" Nullable="false" />
          <NavigationProperty Name="UserExtensionMaps" Relationship="Server.UserUserExtensionMap" FromRole="User" ToRole="UserExtensionMap" />
        </EntityType>
        <EntityType Name="Extension">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
          <Property Type="String" Name="Number" Nullable="false" />
          <NavigationProperty Name="UserExtensionMaps" Relationship="Server.ExtensionUserExtensionMap" FromRole="Extension" ToRole="UserExtensionMap" />
        </EntityType>
        <EntityType Name="UserExtensionMap" >
          <Key>
            <PropertyRef Name="ExtensionId" />
            <PropertyRef Name="UserId" />
          </Key>
          <NavigationProperty Name="Extension" Relationship="Server.ExtensionUserExtensionMap" FromRole="UserExtensionMap" ToRole="Extension" />
          <Property Type="Int32" Name="ExtensionId" Nullable="false" />
          <NavigationProperty Name="User" Relationship="Server.UserUserExtensionMap" FromRole="UserExtensionMap" ToRole="User" />
          <Property Type="Int32" Name="UserId" Nullable="false" />
        </EntityType>
        <Association Name="ExtensionUserExtensionMap">
          <End Type="Server.Extension" Role="Extension" Multiplicity="1" />
          <End Type="Server.UserExtensionMap" Role="UserExtensionMap" Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="Extension">
              <PropertyRef Name="Id" />
            </Principal>
            <Dependent Role="UserExtensionMap">
              <PropertyRef Name="ExtensionId" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
        <Association Name="UserUserExtensionMap">
          <End Type="Server.User" Role="User" Multiplicity="1" />
          <End Type="Server.UserExtensionMap" Role="UserExtensionMap" Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="User">
              <PropertyRef Name="Id" />
            </Principal>
            <Dependent Role="UserExtensionMap">
              <PropertyRef Name="UserId" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
      </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
    <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
  <EntityContainerMapping StorageEntityContainer="ServerStoreContainer" CdmEntityContainer="ServerEntities">
    <EntitySetMapping Name="Users">
      <EntityTypeMapping TypeName="IsTypeOf(Server.User)">
        <MappingFragment StoreEntitySet="Users">
          <ScalarProperty Name="Id" ColumnName="Id" />
          <ScalarProperty Name="Username" ColumnName="Username" />
        </MappingFragment>
      </EntityTypeMapping>
    </EntitySetMapping>
    <EntitySetMapping Name="Extensions">
      <EntityTypeMapping TypeName="IsTypeOf(Server.Extension)">
        <MappingFragment StoreEntitySet="Extensions">
          <ScalarProperty Name="Id" ColumnName="Id" />
          <ScalarProperty Name="Number" ColumnName="Number" />
        </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/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="CodeGenerationStrategy" Value="None" />
      </DesignerInfoPropertySet>
    </Options>
    <!-- Diagram content (shape and connector positions) -->
    <Diagrams></Diagrams>
  </Designer>
</edmx:Edmx>

1 个答案:

答案 0 :(得分:0)

在生成数据库代码right click model -> Generate Database from model之前,我看不到表映射字段,在执行此操作之后映射是正常的。

不确定这是否是一个错误,但它是可重复的并确实解决了问题。