EF 4.1 Code First。每个类型的表继承,其主键名与其基类的主键名不同 [英] EF 4.1 Code First. Table-per-type inheritance with different primary key name from its base class' primary key name

查看:192
本文介绍了EF 4.1 Code First。每个类型的表继承,其主键名与其基类的主键名不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于此:

create table Location(
LocationId int identity(1,1) not null primary key,
Address nvarchar(max) not null,
City nvarchar(max) null,
State nvarchar(max) not null,
ZipCode nvarchar(max) not null
);



create table Park(
ParkId int not null primary key references Location(LocationId),
Name nvarchar(max) not null
);

我试过这个映射:

modelBuilder.Entity<Location>();
modelBuilder.Entity<Park>().ToTable("Park");
modelBuilder.Entity<Park>().Property(x => x.LocationId).HasColumnName("ParkId");

不幸的是,这不起作用。

Unfortunately that didn't work.

using (var db = new Ef())
{
    var park = new Park { Name = "11th Street Park", Address = "801 11th Street", City = "Aledo", State = "TX", ZipCode = "76106" };
    db.Set<Location>().Add(park);

    db.SaveChanges();
}

出现此错误:


属性'LocationId'不是'Park'类型的声明属性。
使用Ignore方法或NotMappedAttribute数据
注释验证是否未从
模型中显式排除该属性。确保它是一个有效的原始属性。

The property 'LocationId' is not a declared property on type 'Park'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.

我应该如何映射Park实体,使其LocationId属性落到ParkId列?

How should I map Park entity so its LocationId property fall to ParkId column?

我顺便提一下这个映射:

I have this mapping by the way:

public class Location
{
    public virtual int LocationId { get; set; }
    public virtual string Address { get; set; }
    public virtual string City { get; set; }
    public virtual string State { get; set; }
    public virtual string ZipCode { get; set; }
}

public class Park : Location
{
    public virtual string Name { get; set; }
}

如果它可以提供帮助,这可以在EF 4.0中实现(通过设计师) ,按照实体框架4.0配方,问题解决方案方法的第2-11章中的步骤进行操作。现在我首先通过EF 4.1尝试代码

If it could help, this is possible in EF 4.0 (via designer), just followed the steps in Chapter 2-11 of Entity Framework 4.0 Recipes, Problem Solution Approach. Now I'm trying it on code first via EF 4.1

如果我将ParkId更改为LocationId,一切正常。但是,通过设计器方法,可以将LocationId映射到表Park的ParkId;我想用代码实现同样的事情

If I change the ParkId to LocationId, things are ok. However, with designer approach, it is possible to map the LocationId to ParkId of table Park; I want to achieve the same thing with code first

create table Park( 
LocationId int not null primary key references Location(LocationId), 
Name nvarchar(max) not null 
); 


推荐答案

据我所知(我多次尝试过)代码首先不支持这个=>你的派生类型应该使用相同的主键名列。

As I know (and I tried it multiple times) code first doesn't support this => your derived type should use same column names for primary key.

这个问题可以很简单地描述:当前流畅的映射实现没有t允许从父实体覆盖映射规则=>父实体定义所有派生实体中主键列的名称。

This problem can be described very simply: Current fluent mapping implementation doesn't allow overriding mapping rules from parent entity => parent entity defines names of primary key columns in all derived entities.

IMO最可能的原因是它实际上是设计为代码首先是您没有现有数据库的地方,而且您不必费心使用数据库命名 - EF需要根据需要定义名称。一旦发布了DbContext API,人们开始大量使用现有数据库。但是这里出现了一个问题:初始用例没有计算在内,所以在EDMX中很容易完成的一些场景是不可能的。这是其中之一。

IMO the most probable reason is that it was really designed as code first where you don't have existing database and you do not have to bother with database naming - it was up to EF to define names as it needed. Once DbContext API was released people started to use it with existing database massively. But here comes a problem: Initial use cases didn't count with this so some scenarios which are pretty easily done in EDMX are not possible. This is one of them.

这篇关于EF 4.1 Code First。每个类型的表继承,其主键名与其基类的主键名不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆