【发布时间】:2011-09-02 03:39:00
【问题描述】:
鉴于此:
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");
很遗憾,这不起作用。
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 数据建模 注解。确保它是一个有效的原始属性。
我应该如何映射 Park 实体,使其 LocationId 属性落入 ParkId 列?
顺便说一句,我有这个映射:
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 在代码上尝试它
[编辑]
如果我将 ParkId 更改为 LocationId,一切正常。但是,通过设计器的方法,可以将 LocationId 映射到表 Park 的 ParkId;我想先用代码实现同样的目标
create table Park(
LocationId int not null primary key references Location(LocationId),
Name nvarchar(max) not null
);
【问题讨论】:
标签: entity-framework inheritance entity-framework-4.1