English 中文(简体)
实体框架4.1 驻地协调员:第一实体法
原标题:Entity Framework 4.1 RC: Code First EntityTypeConfiguration inheritance issue

我正试图利用一个共同的实体类型来配置我所有实体的主要钥匙,以便每个衍生的组合类别不会重复。 我的所有实体都采用了一种共同的界面“意向”(即每个实体必须拥有一种类型的不动产)。

我的组合基阶层对此感到::

public class EntityConfiguration<TEntity> : EntityTypeConfiguration<TEntity>

    where TEntity : class , IEntity {

    public EntityConfiguration() {

        HasKey( e => e.Id );

        Property( e => e.Id ).HasDatabaseGeneratedOption( DatabaseGeneratedOption.Identity );

    }

}

Each entity then has it s own specific configuration class extending this one like this:

public class CustomerConfiguration : EntityConfiguration<Customer> {

    public CustomerConfiguration() : base() {

        // Entity specific configuration here

    }

}

这部法律汇编了罚款,但我面临的问题是,从目前来看,当第4条第1款(驻地协调员)试图建立这一模式时,我会提出以下例外:

System.InvalidOperationException was unhandled Message=The key component Id is not a declared property on type Customer . Verify that it has not been explicitly excluded from the model and that it is a valid primitive property. Source=EntityFramework

如果我改变客户配置类别,从实体TypeConfiguration<Customer>延伸;重复主要的主要配置,那么,客户组合就会奏效,但我失去分享共同配置的能力(主要动力)。

  • Am I doing something wrong here?
  • Is there another way to share common configuration between entities?

这里指的是其他课程:

public interface IEntity {

    int Id { get; set; }

}

public class Customer : IEntity {

    public virtual int Id { get; set; }

    public virtual string name { get; set; }

}

Thanks!

最佳回答

I do not think that you need to go through all of this. EF 4.1 Code First uses a lot of convention over configuration and via this, the Id property of an entity is configured as the primary key. So by implementing the IEntity interface on your entities you are setting them up with the Id as the primary key.

这里与《反歧视法》有联系。 http://blogs.msdn.com/b/ef Design/archive/06/01/conventions-for-code-first.aspx“rel=” 第1条(a)项

问题回答

它认为这些组合有一些问题。 如果您改动<代码>IEntity to EntityBase:

public class EntityBase
{
    public virtual int Id { get; set; }
}

public class Customer : EntityBase
{
    public virtual string Name { get; set; }
}

public class EntityConfiguration<TEntity> : EntityTypeConfiguration<TEntity>
    where TEntity : EntityBase
{
    public EntityConfiguration()
    {
        HasKey(e => e.Id);
        Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
}

public class CustomerConfiguration : EntityConfiguration<Customer>
{
    public CustomerConfiguration()
        : base()
    {
        ...
    }
}

你们只会在某个类别上形成固定的方法,并将实体并入该类别。 例如:

public class CustomerConfiguration : EntityConfiguration<Customer>
{
    public CustomerConfiguration()
        : base()
    {
        ...
        EntityConfiguration.Configure(this);
    }
}

public static class EntityConfiguration
{
    public static void Configure<TEntity>(EntityTypeConfiguration<TEntity> entity) where TEntity : EntityBase
    {
        entity.HasKey(e => e.Id);
        entity.Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
}

I have similar issue with EF5.0 when i have generic abstract class with Id property and implementation for abstract members and self defined properties. look like entity framework code first is looking only for mapped class properties. i have tried to use reflector - seems i am right, but don t sure about this for 100%.

幸运的是,已经找到了解决办法:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {                
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
            modelBuilder.Entity<MyEntity>()
               .Map(m =>
               {
                   **m.MapInheritedProperties();**                   
               });
        }

就我的情况而言:也从基类绘制地产图,即必须增加一条代码m.Map InheritedProperties。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签