EntityTypeConfiguration

EntityTypeConfiguration

本文介绍了使用EntityTypeConfiguration< T>时抽象域模型基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取Base对象属性的中央映射有一些技巧吗?
当使用EntityTypeConfiguration时,抽象类有一些简单的模式。

非常感谢任何提示。
我无法声明一个类

 公共类BaseEntityConfig< T> :EntityTypeConfiguration< T> 

类似的问题,我无法得到工作答案


  public abstract class BosBaseObject 
{
public virtual Guid Id {set;得到; }
public virtual string ExternalKey {set;得到; }
public byte [] RowVersion {get;组; }
}
public class新闻:BosBaseObject
{
public String Heading {set;得到; }
}


public class NewsMap:EntityTypeConfiguration< News>
{
public NewsMap()
{
// Base Object Common Mappings
//我们如何使用所有基本抽象属性的中央映射


}
}
//这样的东西,但对任何建议都很开放....
public class BosBaseEntityConfig< T> :EntityTypeConfiguration< T>
{
public void BaseObjectMap()
{
//主键
this.HasKey(t => t.Id);

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

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

//列映射
this.Property(t => t.Id).HasColumnName(Id);
}
}


解决方案

之后6小时我破解了我认为这是一个相当干净的结果。
诀窍是忘记从EntityTypeConfiguration
派生的类中的每个内部,并构建一个自定义BaseConfig,然后获取此实例并添加此类的具体信息。希望它帮助别人首先使用抽象...

  public abstract class BosBaseObject 
{
public虚拟Guid Id {set;得到; }
public virtual string ExternalKey {set;得到; }
public byte [] RowVersion {get;组; }
}
public abstract class BosObjectDateManaged:BosBaseObject
{
public DateTimeOffset ValidFrom {set;得到; }
public DateTimeOffset ValidTo {set;得到;
}
public class新闻:BosObjectDateManaged
{
public String标题{set;得到;




protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var conf = new BosBaseEntityConfiguration< News>(); //为Type
构造配置modelBuilder.Configurations.Add(conf); //这里现在有基础映射
var newsConf = new NewsConfiguration(conf); //现在对象的特定属性东西

}

}
public class BosBaseEntityConfiguration< T> :EntityTypeConfiguration< T>其中T:BosBaseObject
{
public BosBaseEntityConfiguration()
{
//主键
this.HasKey(t => t.Id);

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

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

//列映射
this.Property(t => t.Id).HasColumnName(Id);
}
}
public class NewsConfiguration
{
public NewsConfiguration(BosBaseEntityConfiguration< News> entity)
{
// Table Specific&列映射
entity.ToTable(News2);
entity.Property(t => t.Heading).HasColumnName(Heading2);
}
}


Is there some trick to getting a central mapping of Base object properties?Is there some simple pattern for abstract classes when using EntityTypeConfiguration.
ANy tips much appreciated.Im unable to declare a class

Public class BaseEntityConfig<T> : EntityTypeConfiguration<T>

Similar issues, where i couldnt get the answers to workHow to create and use a generic class EntityTypeConfiguration<TEntity>and Dynamic way to Generate EntityTypeConfiguration : The type 'TResult' must be a non-nullable value type

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");
    }
}
解决方案

After 6 hrs I cracked it. I think it is a reasonably clean outcome.The trick is to forget doing every inside a class derived from EntityTypeConfigurationand build a custom BaseConfig and then to take this instance and add the specifics for this class. Hope it helps others doing code first with abstracts...

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

这篇关于使用EntityTypeConfiguration&lt; T&gt;时抽象域模型基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 14:33