2

代碼

我在我的應用程序兩個非常簡單的接口如何創建和使用泛型類EntityTypeConfiguration <TEntity>

表示將保存在數據庫實體

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; } 

    .... 
} 

用於例如問題

完全不同的領域

Ord呃,以促進(而不必創建一個配置類爲每個新類)創建了一個通用類配置:

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. 

回答

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 s添加到上下文或通過提供它們自己的配置類,否則您將再次得到相同的異常。)

也許,抽象基類是替代品。否則,我擔心,您需要爲每個實體創建具體的配置類以擺脫該異常。

+0

謝謝,它的工作! – ridermansb