根据这个消息来源
http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph。 ASPX
应该可以让TPH鉴别器列为整数:
此外,更改鉴别器列的数据类型很有意思。 在上面的代码中,我们将字符串传递给HasValue方法,但是此方法已被定义为接受一种对象:
public void HasValue(object value);
因此,例如,如果我们将int类型的值传递给它,那么Code First不仅在鉴别器列中使用我们期望的值(即1和2),而且还将列类型更改为(INT,NOT NULL) :
modelBuilder.Entity()。Map(m => m.Requires(“BillingDetailType”)。HasValue(1))。Map(m => m.Requires(“BillingDetailType”)。HasValue(2));
但是,当我在我的代码中执行此操作时,我会看到“1”和“2”之类的鉴别器值,但列类型仍然存在
nvarchar(128),不为null
实际上是否可以指定整数鉴别器列? 如果是这样,怎么样?
我确定我将我的映射指定为.HasValue(1)而不是.HasValue(“1”) 。
According to this source
http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx
it should be possible to have the TPH discriminator column be an integer:
Also, changing the data type of discriminator column is interesting. In the above code, we passed strings to HasValue method but this method has been defined to accepts a type of object:
public void HasValue(object value);
Therefore, if for example we pass a value of type int to it then Code First not only use our desired values (i.e. 1 & 2) in the discriminator column but also changes the column type to be (INT, NOT NULL):
modelBuilder.Entity() .Map(m => m.Requires("BillingDetailType").HasValue(1)) .Map(m => m.Requires("BillingDetailType").HasValue(2));
However, when I do that in my code I see discriminator values like "1" and "2", but the column type is still
nvarchar(128), not null
Is it in fact possible to specify an integer discriminator column? If so, how?
I'm certain that I specify my mapping as .HasValue(1) and not .HasValue("1").
最满意答案
modelBuilder.Entity<X>() .Map<X>(m => { m.Requires("BillingDetailType").HasValue(0).HasColumnType("tinyint"); }) .Map<Y>(m => { m.Requires("BillingDetailType").HasValue(1); m.MapInheritedProperties(); }) .Map<Z>(m => { m.Requires("BillingDetailType").HasValue(2); m.MapInheritedProperties(); }) ; modelBuilder.Entity<X>() .Map<X>(m => { m.Requires("BillingDetailType").HasValue(0).HasColumnType("tinyint"); }) .Map<Y>(m => { m.Requires("BillingDetailType").HasValue(1); m.MapInheritedProperties(); }) .Map<Z>(m => { m.Requires("BillingDetailType").HasValue(2); m.MapInheritedProperties(); }) ;更多推荐
发布评论