2011-05-17 66 views
0

我有以下的POCO類:實體框架 - 加載實體的標識沒有被分配

public class Person : Entity 
    { 
     public string FirstName { get; set; } 
     public string MiddleName1 { get; set; } 
     public string MiddleName2 { get; set; } 
     public string LastName { get; set; } 
     public byte? DayOfBirth { get; set; } 
     public byte? MonthOfBirth { get; set; } 
     public Int16? YearOfBirth { get; set; } 
     public string MobileNumber { get; set; } 
    } 

public abstract class Entity 
{ 
    public int Id { get; set; } 
} 

以下是相應的EDMX XML:

<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="EntityFramework.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="EntityFrameworkStoreContainer"> 
      <EntitySet Name="People" EntityType="EntityFramework.Store.People" store:Type="Tables" Schema="dbo" /> 
     </EntityContainer> 
     <EntityType Name="People"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="int" Nullable="false" /> 
      <Property Name="FirstName" Type="nvarchar" Nullable="false" MaxLength="50" /> 
      <Property Name="MiddleName1" Type="nvarchar" MaxLength="50" /> 
      <Property Name="MiddleName2" Type="nvarchar" MaxLength="50" /> 
      <Property Name="LastName" Type="nvarchar" Nullable="false" MaxLength="50" /> 
      <Property Name="DayOfBirth" Type="tinyint" /> 
      <Property Name="MonthOfBirth" Type="tinyint" /> 
      <Property Name="YearOfBirth" Type="smallint" /> 
      <Property Name="MobileNumber" Type="varchar" MaxLength="20" /> 
     </EntityType> 
     </Schema> 
    </edmx:StorageModels> 
    <!-- CSDL content --> 
    <edmx:ConceptualModels> 
     <Schema Namespace="EntityFramework" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm"> 
     <EntityContainer Name="TheCleavesEntities" annotation:LazyLoadingEnabled="true"> 
      <EntitySet Name="People" EntityType="EntityFramework.Person" /> 
     </EntityContainer> 
     <EntityType Name="Person"> 
      <Key> 
      <PropertyRef Name="Id" /> 
      </Key> 
      <Property Name="Id" Type="Int32" Nullable="false" /> 
      <Property Name="FirstName" Type="String" Nullable="false" MaxLength="50" Unicode="true" FixedLength="false" /> 
      <Property Name="MiddleName1" Type="String" MaxLength="50" Unicode="true" FixedLength="false" /> 
      <Property Name="MiddleName2" Type="String" MaxLength="50" Unicode="true" FixedLength="false" /> 
      <Property Name="LastName" Type="String" Nullable="false" MaxLength="50" Unicode="true" FixedLength="false" /> 
      <Property Name="DayOfBirth" Type="Byte" Nullable="true" /> 
      <Property Name="MonthOfBirth" Type="Byte" Nullable="true" /> 
      <Property Name="YearOfBirth" Type="Int16" Nullable="true" /> 
      <Property Name="MobileNumber" Type="String" MaxLength="20" Unicode="false" FixedLength="false" /> 
     </EntityType> 
     </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="EntityFrameworkStoreContainer" CdmEntityContainer="TheCleavesEntities"> 
      <EntitySetMapping Name="People"><EntityTypeMapping TypeName="EntityFramework.Person"><MappingFragment StoreEntitySet="People"> 
      <ScalarProperty Name="Id" ColumnName="Id" /> 
      <ScalarProperty Name="FirstName" ColumnName="FirstName" /> 
      <ScalarProperty Name="MiddleName1" ColumnName="MiddleName1" /> 
      <ScalarProperty Name="MiddleName2" ColumnName="MiddleName2" /> 
      <ScalarProperty Name="LastName" ColumnName="LastName" /> 
      <ScalarProperty Name="DayOfBirth" ColumnName="DayOfBirth" /> 
      <ScalarProperty Name="MonthOfBirth" ColumnName="MonthOfBirth" /> 
      <ScalarProperty Name="YearOfBirth" ColumnName="YearOfBirth" /> 
      <ScalarProperty Name="MobileNumber" ColumnName="MobileNumber" /> 
      </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="False" /> 
     </DesignerInfoPropertySet> 
    </Options> 
    <!-- Diagram content (shape and connector positions) --> 
    <Diagrams> 
     <Diagram Name="TheCleaves"> 
     <EntityTypeShape EntityType="EntityFramework.Person" Width="1.5" PointX="0.75" PointY="0.75" Height="2.7494921874999996" IsExpanded="true" /> 
     </Diagram> 
    </Diagrams> 
    </Designer> 
</edmx:Edmx> 

當我加載一個實體(也僅是目前數據庫中有一行)用代碼:

IEnumerable<Person> people = context.People.Where(x => true); 

我發現返回的Id屬性Person對象爲0(在數據庫中爲1)。任何人都有任何想法,爲什麼它似乎沒有設置?

感謝

+0

是否存在與您的實體類相對應的映射?你是否期望所有的Ids都是全球獨一無二或獨一無二的類型?即如果您有人員和產品,是否可以有Id爲1的產品和Id爲1的人員?你想使用繼承來共享行爲,或者一個接口是否可以完成這項工作? (實體框架使用繼承進行事務處理,它不會與接口一起使用) – RichardW1001 2011-05-17 08:15:40

+0

只適用於該類型。我使用繼承來共享行爲(它在語義上也是合理的 - 實體是特定類的類)。你認爲這個Id沒有被填充,因爲它來自基類嗎? – David 2011-05-17 18:22:41

回答

2

確定它是不是繼承的問題,因爲我最初以爲 - 繼承會工作。這很可能是兩個問題的組合:

  • Id在數據庫中生成但EF不知道它們。 EDMX的SSDL和CSDL部分應該是define Id with StoreGeneratedPattern.Identity。這會強制EF在插入實體時重新加載Id。
  • 我相信你正在使用相同的上下文實例來保存實體並調用查詢。現在你遇到了identity map pattern。儘管從查詢中檢索到的數據EF將使用內部存儲在其每個上下文緩存中的實例。由於第一個問題,緩存實例有Id,默認值爲int值= 0.
+0

這是真的嗎?我無法相信EF會對我的領域設計決策產生太大的影響。我一直在用NHibernate使用這種模式,而NH甚至沒有揚起眉毛。 – David 2011-05-17 18:25:04

+0

當我更改類以便它不擴展實體並直接將Id屬性添加到類時,我仍然遇到完全相同的問題。 – David 2011-05-17 18:43:05

+0

您是否使用相同的上下文實例進行保存和加載? – 2011-05-17 18:45:32