6

获取 Base 对象属性的中央映射是否有一些技巧?使用 EntityTypeConfiguration 时是否有一些抽象类的简单模式。
非常感谢任何提示。我无法声明一个类

Public class BaseEntityConfig<T> : EntityTypeConfiguration<T>

类似的问题,我无法获得工作的答案 如何创建和使用通用类 EntityTypeConfiguration<TEntity>生成 EntityTypeConfiguration 的动态方式:类型“TResult”必须是不可为空的值类型

public  abstract class BosBaseObject
{
  public virtual Guid Id { set; get; }
  public virtual string ExternalKey { set; get; }
  public byte[] RowVersion { get; set; }
}
  public class News : BosBaseObject
{
    public String Heading { set; get; }
}


public class NewsMap : EntityTypeConfiguration<News>
{
    public NewsMap()
    {
      //Base Object Common Mappings
      // How can we use a central mapping for all Base Abstract properties  


     }
 }
// Something like this but very open to any suggestion....
public class BosBaseEntityConfig<T> : EntityTypeConfiguration<T>
{
  public void BaseObjectMap( )
    { 
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.Id).HasDatabaseGeneratedOption(databaseGeneratedOption: DatabaseGeneratedOption.None);

        this.Property(t => t.RowVersion)
            .IsRequired()
            .IsFixedLength()
            .HasMaxLength(8)
            .IsRowVersion();

        //Column Mappings
        this.Property(t => t.Id).HasColumnName("Id");
    }
}
4

3 回答 3

9

上面的答案肯定有效,尽管这可能稍微干净一些,并且在 DbContext 中注册配置时具有相同的工作优势。

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

public class Company : BaseEntity
{
    public string Name { get; set; }
}

internal class BaseEntityMap<T> : EntityTypeConfiguration<T> where T : BaseEntity
{
    public BaseEntityMap()
    {
        // Primary Key
        HasKey(t => t.Id);
    }
}

internal class CompanyMap : BaseEntityMap<Company>
{
    public CompanyMap()
    {
        // Properties
        Property(t => t.Name)
            .IsRequired()
            .HasMaxLength(256);
    }
}

public class AcmeContext : DbContext
{
    public DbSet<Company> Companies { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new CompanyMap());
    }
}

一天一大早,克里斯蒂安·威廉姆斯和我自己就提出了上述解决方案……

于 2013-08-16T09:28:01.090 回答
3

6小时后,我破解了它。我认为这是一个相当干净的结果。诀窍是忘记在派生自 EntityTypeConfiguration 的类中执行所有操作并构建自定义 BaseConfig,然后获取此实例并添加该类的细节。希望它可以帮助其他人先用摘要做代码......

public  abstract class BosBaseObject
{
  public virtual Guid Id { set; get; }
  public virtual string ExternalKey { set; get; }
  public byte[] RowVersion { get; set; }
}
 public abstract class BosObjectDateManaged   :  BosBaseObject
{
    public DateTimeOffset ValidFrom { set; get; }
    public DateTimeOffset ValidTo { set; get; }
}
public class News : BosObjectDateManaged
{
    public String Heading { set; get; }
}



protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        var conf = new BosBaseEntityConfiguration<News>();//Construct config for Type
        modelBuilder.Configurations.Add( conf );  // this has base mapping now
        var newsConf = new NewsConfiguration(conf); // now the Object specific properties stuff

    }

}
public class BosBaseEntityConfiguration<T> : EntityTypeConfiguration<T> where T : BosBaseObject
{
   public BosBaseEntityConfiguration()
   {
       // Primary Key
       this.HasKey(t => t.Id);

       //// Properties
       this.Property(t => t.Id).HasDatabaseGeneratedOption(databaseGeneratedOption: DatabaseGeneratedOption.None);

       this.Property(t => t.RowVersion)
           .IsRequired()
           .IsFixedLength()
           .HasMaxLength(8)
           .IsRowVersion();

       //Column Mappings
       this.Property(t => t.Id).HasColumnName("Id");
   }
}
 public class NewsConfiguration  
{
    public  NewsConfiguration(BosBaseEntityConfiguration<News> entity)
    {
        // Table Specific & Column Mappings
        entity.ToTable("News2");
        entity.Property(t => t.Heading).HasColumnName("Heading2");
    }
}
于 2012-11-18T12:26:57.767 回答
0

抱歉,我无法发表评论,但我会像你一样只交换这两行

      modelBuilder.Configurations.Add( conf );  // this has base mapping now
      var newsConf = new NewsConfiguration(conf); // now the Object specific properties stuff
   to
       new NewsConfiguration(conf); // now the Object 
       modelBuilder.Configurations.Add( conf );  // this has base mapping now

这有助于 EF 的专业领域。

于 2016-02-16T15:00:56.293 回答