Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 4 实体框架-未分配已加载实体的Id_Entity Framework 4 - Fatal编程技术网

Entity framework 4 实体框架-未分配已加载实体的Id

Entity framework 4 实体框架-未分配已加载实体的Id,entity-framework-4,Entity Framework 4,我有以下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 b

我有以下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);
IEnumerable people=context.people.Where(x=>true);
我发现返回的Person对象的Id属性是0(在数据库中是1)。有人知道为什么看起来没有设置吗


谢谢

好的,继承并不像我最初想的那样有问题——继承会起作用的。这很可能是两个问题的组合:

  • Id
    在数据库中生成,但EF不知道它们。EDMX的SSDL和CSDL部分都应包含。这将强制EF在插入实体时重新加载Id
  • 我相信您正在使用相同的上下文实例来保存实体和调用查询。现在你们见面了。尽管从查询中检索到数据,EF将使用内部存储在其每上下文缓存中的实例。由于第一个问题,缓存实例具有默认值为0的
    Id

是否存在与实体类对应的映射?您希望所有ID都是全局唯一的还是该类型的唯一ID?i、 e.如果您有个人和产品,是否会有Id为1的产品和Id为1的人?您是想使用继承来共享行为,还是一个接口来完成这项工作?(实体框架通过继承完成它在接口中不做的事情)ID对于类型来说是唯一的。我使用继承来共享行为(在语义上也是合理的——实体是一种特殊类型的类)。你认为这个Id不是因为来自基类而被填充的吗?真的是这样吗?我不敢相信EF会对我的领域设计决策造成如此大的干扰。我一直在NHibernate中使用这种模式,NH甚至没有引起注意。当我更改类以使其不扩展实体并将Id属性直接添加到类中时,我仍然会遇到完全相同的问题。是否使用相同的上下文实例进行保存和加载?我没有保存。这个特定的上下文实例只是加载一个要在列表中显示的Person实例列表。顺便说一句,我喜欢你的想法,但是这一行在数据库中已经存在很多年了,我现在正在尝试如何加载它!