1

我有點卡住了工作出錯的地方,下面的實體框架映射。我所做的是:EntityFramework映射 - 這個映射有什麼問題?

一)創建一個SQLite數據庫(見下文,指出SQLite不容許FK約束) B)創建一個空的實體數據模式&然後創建從數據庫 C型式)問題然後試圖添加模型關聯,以便它拾取並使用我在USAGES表中的數據庫中創建的「ProcessNameId」列。我一直在嘗試使用GUI表格映射窗格來做到這一點,但它看起來不太直觀。

問題 - 我發現了以下錯誤:

Error 3 
Error 3021: Problem in mapping fragments starting at line 85: 
Each of the following columns in table Usages is mapped to multiple conceptual side properties: 

Usages.ProcessNameId is mapped to <ProcessNameUsage.ProcessName.Id, ProcessNameUsage.Usage.Id> 
Error 4 
Error 3025: Problem in mapping fragments starting at line 85: 
Must specify mapping for all key properties (Usages.Id) of table Usages. 

問題 - 所以,問題是什麼改變爲XML,我需要做出正確有關聯的工作?

SQL

CREATE TABLE "ProcessNames" (
    "Id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , 
    "Name" VARCHAR NOT NULL UNIQUE 
) 

CREATE TABLE "Usages" (
    "Id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , 
    "ProcessNameId" INTEGER NOT NULL , 
    "Amount" INTEGER NOT NULL , 
    "Datetime" DATETIME NOT NULL 
) 

型號配置

<?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="Model1.Store" Alias="Self" Provider="System.Data.SQLite" ProviderManifestToken="ISO8601" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl"> 
     <EntityContainer Name="Model1StoreContainer"> 
      <EntitySet Name="ProcessNames" EntityType="Model1.Store.ProcessNames" store:Type="Tables" /> 
      <EntitySet Name="Usages" EntityType="Model1.Store.Usages" store:Type="Tables" /> 
     </EntityContainer> 
     <EntityType Name="ProcessNames"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="integer" Nullable="false" StoreGeneratedPattern="Identity" /> 
      <Property Name="Name" Type="nvarchar" Nullable="false" /> 
     </EntityType> 
     <EntityType Name="Usages"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="integer" Nullable="false" StoreGeneratedPattern="Identity" /> 
      <Property Name="ProcessNameId" Type="integer" Nullable="false" /> 
      <Property Name="Amount" Type="integer" Nullable="false" /> 
      <Property Name="Datetime" Type="datetime" Nullable="false" /> 
     </EntityType> 
     </Schema></edmx:StorageModels> 
    <!-- CSDL content --> 
    <edmx:ConceptualModels> 
     <Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="Model1" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation"> 
     <EntityContainer Name="Model1Container" annotation:LazyLoadingEnabled="true"> 
      <EntitySet Name="ProcessNames" EntityType="Model1.ProcessName" /> 
      <EntitySet Name="Usages" EntityType="Model1.Usage" /> 
      <AssociationSet Name="ProcessNameUsage" Association="Model1.ProcessNameUsage"> 
      <End Role="ProcessName" EntitySet="ProcessNames" /> 
      <End Role="Usage" EntitySet="Usages" /> 
      </AssociationSet> 
     </EntityContainer> 
     <EntityType Name="ProcessName"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Type="Int64" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" /> 
      <Property Type="String" Name="Name" Nullable="false" MaxLength="2147483647" FixedLength="false" Unicode="true" /> 
      <NavigationProperty Name="Usages" Relationship="Model1.ProcessNameUsage" FromRole="ProcessName" ToRole="Usage" /> 
     </EntityType> 
     <EntityType Name="Usage"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Type="Int64" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" /> 
      <Property Type="Int64" Name="ProcessNameId" Nullable="false" /> 
      <Property Type="Int64" Name="Amount" Nullable="false" /> 
      <Property Type="DateTime" Name="Datetime" Nullable="false" /> 
      <NavigationProperty Name="ProcessName" Relationship="Model1.ProcessNameUsage" FromRole="Usage" ToRole="ProcessName" /> 
     </EntityType> 
     <Association Name="ProcessNameUsage"> 
      <End Type="Model1.ProcessName" Role="ProcessName" Multiplicity="1" /> 
      <End Type="Model1.Usage" Role="Usage" Multiplicity="*" /> 
     </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="Model1StoreContainer" CdmEntityContainer="Model1Container"> 
      <EntitySetMapping Name="ProcessNames"> 
      <EntityTypeMapping TypeName="Model1.ProcessName"> 
       <MappingFragment StoreEntitySet="ProcessNames"> 
       <ScalarProperty Name="Name" ColumnName="Name" /> 
       <ScalarProperty Name="Id" ColumnName="Id" /> 
       </MappingFragment> 
      </EntityTypeMapping> 
      </EntitySetMapping> 
      <EntitySetMapping Name="Usages"> 
      <EntityTypeMapping TypeName="Model1.Usage"> 
       <MappingFragment StoreEntitySet="Usages"> 
       <ScalarProperty Name="Datetime" ColumnName="Datetime" /> 
       <ScalarProperty Name="Amount" ColumnName="Amount" /> 
       <ScalarProperty Name="ProcessNameId" ColumnName="ProcessNameId" /> 
       <ScalarProperty Name="Id" ColumnName="Id" /> 
       </MappingFragment> 
      </EntityTypeMapping> 
      </EntitySetMapping> 
      <AssociationSetMapping Name="ProcessNameUsage" TypeName="Model1.ProcessNameUsage" StoreEntitySet="Usages"> 
      <EndProperty Name="ProcessName"> 
       <ScalarProperty Name="Id" ColumnName="ProcessNameId" /> 
      </EndProperty> 
      <EndProperty Name="Usage"> 
       <ScalarProperty Name="Id" ColumnName="ProcessNameId" /> 
      </EndProperty> 
      </AssociationSetMapping> 
     </EntityContainerMapping> 
</Mapping></edmx:Mappings> 
    </edmx:Runtime> 
    <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) --> 
    <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2008/10/edmx"> 
    <edmx:Connection> 
     <DesignerInfoPropertySet> 
     <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" /> 
     </DesignerInfoPropertySet> 
    </edmx:Connection> 
    <edmx:Options> 
     <DesignerInfoPropertySet> 
     <DesignerProperty Name="ValidateOnBuild" Value="true" /> 
     <DesignerProperty Name="EnablePluralization" Value="True" /> 
     <DesignerProperty Name="IncludeForeignKeysInModel" Value="False" /> 
     </DesignerInfoPropertySet> 
    </edmx:Options> 
    <!-- Diagram content (shape and connector positions) --> 
    <edmx:Diagrams> 
     <Diagram Name="Model1" > 
     <EntityTypeShape EntityType="Model1.ProcessName" Width="1.5" PointX="0.75" PointY="0.75" Height="1.2636116536458317" /> 
     <EntityTypeShape EntityType="Model1.Usage" Width="1.5" PointX="3" PointY="0.75" Height="1.7566536458333339" /> 
     <AssociationConnector Association="Model1.ProcessNameUsage"> 
      <ConnectorPoint PointX="2.25" PointY="1.4639794921875002" /> 
      <ConnectorPoint PointX="3" PointY="1.4639794921875002" /> 
     </AssociationConnector> 
     </Diagram> 
    </edmx:Diagrams> 
    </edmx:Designer> 
</edmx:Edmx> 

感謝

+1

有什麼問題嗎? – Bart 2010-09-23 23:08:56

+0

澄清補充 – Greg 2010-09-24 04:08:50

回答

2

如果要公開外鍵ID和也有EF管理的關係,你可以使用外鍵關聯,但這些只適用於EF 4.0

1

已經解決了它。因此,爲了得到它從SQLite數據庫導入模型後的工作我做了以下內容:

一)刪除進口「ProcessNameId」列

B),然後在此之後添加了兩個實體之間的關聯 - 它自動使用「ProcessNameId」爲FK ID這是好事,因爲它相匹配的,是我已經有了在數據庫中(否則我將不得不去追逐這件事)

事以後這工作得很好。

+1

這最終讓我朝着正確的方向努力。在我的情況下,我不得不將新創建的屬性重新映射到數據庫中的現有屬性,因爲它不可爲空並且沒有默認值。即使這是3年前的答案,但我今天在EF 6中遇到了同樣的問題。 – 2014-04-07 18:39:38