如何创建和使用通用类EntityTypeConfiguration< TEntity> [英] How to create and use a generic class EntityTypeConfiguration<TEntity>
问题描述
我的应用程序中有两个非常简单的界面
I have two very simple interfaces in my application
保存在数据库中
public interface IEntity
{
int Id { get; set; }
}
只有 Nome的实体
字段作为必填字段保存实体
Entities that have only the Nome
field as required field to save the entity
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; }
....
}
h1>
为方便(而不必为每个新类创建配置类),创建了一个通用类配置:
Problem
Order to facilitate (and not have to create a configuration class for each new class) created a generic class configuration:
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);
}
}
这样,我的设置, / strong>
This way, my setup, would
modelBuilder.Configurations.Add(new EntityNameConfiguracao<Modalidade>());
modelBuilder.Configurations.Add(new EntityNameConfiguracao<TipoPessoa>());
modelBuilder.Configurations.Add(new EntityNameConfiguracao<Nacionalidade>());
modelBuilder.Configurations.Add(new EntityNameConfiguracao<Profissao>());
错误
但是,当尝试添加新的迁移,出现以下错误:
Error
However, when trying to add a new migration, the following error occurs:
命令
软件包管理器控制台:添加迁移PessoaDadosAdicionais
Command in Package Manager Console: 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.
推荐答案
我无法解释为什么它不起作用。它不是。它不适用于EF 4.1。
I cannot explain why it doesn't work. It just doesn't. It doesn't work for EF 4.1 either.
这是特别令人惊讶的,因为它将接口转换为抽象基类时起作用:
This is especially surprising because it works when you convert the interfaces to abstract base classes:
public abstract class BaseEntity
{
public int Id { get; set; }
}
public abstract class BaseEntityName : BaseEntity
{
public string Nome { get; set; }
}
public class Modalidade : BaseEntityName
{
}
然后将通用配置更改为:
And then change the generic configuration to:
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);
}
}
现在,向模型构建器添加一个具体配置不要抛出异常:
Now, adding a concrete configuration to the model builder doesn't throw an exception:
modelBuilder.Configurations.Add(new EntityNameConfiguracao<Modalidade>());
(您不能添加 BaseEntity
code> BaseEntityName 到模型中,例如通过将这些类添加到上下文或通过提供自己的配置类添加 DbSet
否则你会再次获得相同的异常。)
(You must not add BaseEntity
and BaseEntityName
to the model, for example by adding DbSet
s for these classes to the context or by providing their own configuration classes, otherwise you'll get the same exception again.)
也许,抽象基础类是你的替代品。否则,我恐怕你需要为每个实体创建具体的配置类来摆脱异常。
Maybe, abstract base classes are alternatives for you. Otherwise, I am afraid, you need to create concrete configuration classes for every entity to get rid of the exception.
这篇关于如何创建和使用通用类EntityTypeConfiguration< TEntity>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!