实体框架4.1 RC:代码首先EntityTypeConfiguration继承问题 [英] Entity Framework 4.1 RC: Code First EntityTypeConfiguration inheritance issue

查看:522
本文介绍了实体框架4.1 RC:代码首先EntityTypeConfiguration继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个共同的EntityTypeConfiguration类来配置所有我的实体的主键,让每一个派生配置类不会重演。我所有的实体实现一个共同的接口IEntity(它说,每个实体必须有类型为int的ID属性)



我的配置基础类看起来是这样的:

 公共类EntityConfiguration< TEntity> :EntityTypeConfiguration< TEntity> 

其中TEntity:类,IEntity {

公共EntityConfiguration(){

HasKey(E => e.Id);

房产(E => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

}

}

每个实体然后有它的扩展这一个像这样的具体配置类:

 公共类CustomerConfiguration:EntityConfiguration<客户> {

公共CustomerConfiguration():基地(){

//实体的具体配置这里

}

}

它编译罚款,但我遇到的问题是,在运行时出现以下异常被提出当EF 4.1 RC试图创建模型:




System.InvalidOperationException了
未处理消息=的关键组件
ID不上
型客户声明的属性。验证它有
未明确从
模型的排斥,这是一个有效的原始
属性。来源=的EntityFramework




如果我改变CustomerConfiguration类从EntityTypeConfiguration<延伸;客户>重复的主键的配置,然后它工作正常,但我失去了共享公共配置(DRY主要是动机)的能力。




  • 我在这里做得不对?

  • 有另一种方式来分享实体之间?



常见的配置

有关参考这里所涉及的其他类:

 公共接口IEntity {

INT标识{得到;组; }

}

公共类客户:IEntity {

公共虚拟INT标识{搞定;组; }

公共虚拟字符串名称{搞定;组; }

}



谢谢!


< DIV CLASS =h2_lin>解决方案

我不认为你需要经历这一切。 EF 4.1代码第一次使用了大量的约定优于配置,并通过这一点,实体的ID属性配置作为主键。因此,通过实施对你的实体IEntity接口要设置它们的ID作为主键。



下面是对ADO.NET团队博客解释链接主键惯例是如何工作的 - 约定的Code First


I am trying to use a common EntityTypeConfiguration class to configure the primary key for all of my entities, so that each derived configuration class does not repeat itself. All of my entities implement a common interface IEntity (which says that each entity must have an Id property of type int).

My configuration base class looks like this:

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

    }

}

It compiles fine, but the problem I am having is that at runtime I get the following Exception being raised when EF 4.1 RC tries to create the model:

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

If I change the CustomerConfiguration class to extend from EntityTypeConfiguration<Customer> and repeat the primary key configuration then it works fine, but I lose the ability to share common configuration (DRY principal is the motivation).

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

For reference here are the other classes involved:

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.

Here is a link to the ADO.NET Team Blog that explains how the primary key convention works - Conventions for Code First

这篇关于实体框架4.1 RC:代码首先EntityTypeConfiguration继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆