获取Base对象属性的中心映射是否有一些技巧? 使用EntityTypeConfiguration时是否有一些抽象类的简单模式 非常感谢。 我无法申报一个班级
Public class BaseEntityConfig<T> : EntityTypeConfiguration<T>
类似的问题,我无法得到工作的答案 How to create and use a generic class EntityTypeConfiguration<TEntity> 和 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");
}
}
答案 0 :(得分:7)
上面的答案肯定有效,虽然这可能会更清晰,并且在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());
}
}
上面的解决方案是克里斯蒂安威廉姆斯和我一天早上到达的......
答案 1 :(得分: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");
}
}
答案 2 :(得分: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与专业领域。