问题描述
大家好,
假设我有一个简单的Hierarchy抽象BaseClass,SubClass并且想要使用EntityTypeConfigurations。
建议的方法是什么:
Suppose I have a simple Hierarchy abstract BaseClass, SubClass and want to use EntityTypeConfigurations.
What's the recommended approach:
1.创建:BaseClassConfiguration:EntityTypeConfiguration< BaseClass>和一个类SubClassConfiguration:EntityTypeConfiguration< SubClass>
然后,SubClassConfiguration将调用MapInheritedProperties()来映射BaseClass的属性
2.创建BaseClassConfiguration< T> :EntityTypeConfiguration< BaseClass>和一个类SubClassConfiguration:BaseClassConfiguration< SubClass>
因此SubClassConfiguration自动调用BaseClassConfiguration ctor。测试显示不需要调用MapInheritedProperties。
$
问候,
Koen
1. Create: BaseClassConfiguration : EntityTypeConfiguration<BaseClass> and a class SubClassConfiguration : EntityTypeConfiguration<SubClass>
The SubClassConfiguration would then call MapInheritedProperties() to map the BaseClass's properties
2. Create BaseClassConfiguration<T> : EntityTypeConfiguration<BaseClass> and a class SubClassConfiguration : BaseClassConfiguration<SubClass>
So the SubClassConfiguration automatically calls the BaseClassConfiguration ctor. Tests showed calling MapInheritedProperties is not required.
Regards,
Koen
推荐答案
以下是适合我的,你可以参考一下,评论出来是针对ctp4
the following is working for me , u can refer it, comment out is for ctp4
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Data.Entity.ModelConfiguration;
namespace
MVC3RealEstate.Models
{
public
class
LocationConfiguration
: EntityTypeConfiguration
<Location
>
{
public
LocationConfiguration()
{
this
.HasKey(x => x.ID);
this
.Map<LocationHouse
>(x => //new
{
x.MapInheritedProperties();
x.ToTable("LocationHouses"
);
//ID = x.ID,
//DateCreated = x.DateCreated,
//DateModified = x.DateModified,
//CreatedBy = x.CreatedBy,
//ModifiedBy = x.ModifiedBy,
//Name = x.Name,
//Showable = x.Showable,
//Latitude = x.Latitude,
//Longitude = x.Longitude
}); //.ToTable("LocationHouses");
this
.Map<LocationIndividual
>(x => //new
{
x.MapInheritedProperties();
x.ToTable("LocationIndividuals"
);
//ID = x.ID,
//DateCreated = x.DateCreated,
//DateModified = x.DateModified,
//CreatedBy = x.CreatedBy,
//ModifiedBy = x.ModifiedBy,
//Name = x.Name,
//Showable = x.Showable,
//Latitude = x.Latitude,
//Longitude = x.Longitude
});//.ToTable("LocationIndividuals");
//this.MapHierarchy().Case<LocationIndividual>(x => new
//{
// ID = x.ID,
// DateCreated = x.DateCreated,
// DateModified = x.DateModified,
// CreatedBy = x.CreatedBy,
// ModifiedBy = x.ModifiedBy,
// Name = x.Name,
// Showable = x.Showable,
// Latitude = x.Latitude,
// Longitude = x.Longitude
//}).ToTable("LocationIndividuals");
}
}
}
这篇关于CTP5:继承的EntityTypeConfiguration(TPC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!