如何创建和使用泛型类EntityTypeConfiguration <tentity> </tentity>

时间:2012-10-06 18:05:16

标签: ef-code-first entity-framework-5 ef-migrations

代码

我的应用程序中有两个非常简单的接口

表示保存在数据库中的实体

public interface IEntity
{
    int Id { get; set; }
}

只有Nome字段作为保存实体的必填字段的实体

public interface IEntityName : IEntity
{
    string Nome { get; set; }
}   

许多实现此界面的类

public class Modalidade : IEntityName
{
    public int Id { get; set; }
    public string Nome { get; set; }
}
public class Nacionalidade : IEntityName
{
    public int Id { get; set; }
    public string Nome { get; set; }
}

public class Profissao : IEntityName
{
    public int Id { get; set; }
    public string Nome { get; set; }
}

public class TipoPessoa : IEntityName
{
    public int Id { get; set; }
    public string Nome { get; set; }
}

虽然它们的结构相同,但在本课程中使用的字段完全不同

public class Pessoa : IEntity
{
    public int Id { get; set; }

    public string CPF { get; set; }
    public string PIS { get; set; }
    public string RG { get; set; }
    public string OrgaoExpedidor { get; set; }
    public string TipoDocumento { get; set; }
    public DateTime? DataEmissao { get; set; }

    public virtual Nacionalidade Nacionalidade { get; set; }
    public virtual Profissao Profissao { get; set; }

    public virtual TipoPessoa Tipo { get; set; }

    ....
}

问题

为了方便(并且不必为每个新类创建配置类),创建了一个通用的类配置:

internal class EntityNameConfiguracao<TEntity> : EntityTypeConfiguration<TEntity>
    where TEntity: class, IEntityName
{
    public EntityNameConfiguracao()
    {
        Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(p => p.Nome).IsRequired().HasMaxLength(255);
    }
}

这样,我的设置就会

modelBuilder.Configurations.Add(new EntityNameConfiguracao<Modalidade>());
modelBuilder.Configurations.Add(new EntityNameConfiguracao<TipoPessoa>());
modelBuilder.Configurations.Add(new EntityNameConfiguracao<Nacionalidade>());
modelBuilder.Configurations.Add(new EntityNameConfiguracao<Profissao>());   

错误

但是,在尝试添加新迁移时,会发生以下错误:

命令

程序包管理器控制台中的命令:Add-Migration PessoaDadosAdicionais

错误

The property 'Id' is not a declared property on type 'Modalidade'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.

1 个答案:

答案 0 :(得分:6)

我无法解释为什么它不起作用。它只是没有。它对EF 4.1也不起作用。

这尤其令人惊讶,因为它在将接口转换为抽象基类时起作用:

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

public abstract class BaseEntityName : BaseEntity
{
    public string Nome { get; set; }
}

public class Modalidade : BaseEntityName
{
}

然后将通用配置更改为:

internal class EntityNameConfiguracao<TEntity> : EntityTypeConfiguration<TEntity>
    where TEntity: BaseEntityName
{
    public EntityNameConfiguracao()
    {
        Property(p => p.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(p => p.Nome).IsRequired().HasMaxLength(255);
    }
}

现在,向模型构建器添加具体配置不会引发异常:

modelBuilder.Configurations.Add(new EntityNameConfiguracao<Modalidade>());

(您不能将BaseEntityBaseEntityName添加到模型中,例如将这些类的DbSet添加到上下文中,或者通过提供自己的配置类,否则您将'我会再次得到同样的例外。)

也许,抽象基类可以替代你。否则,我担心,您需要为每个实体创建具体的配置类以消除异常。