开发者

EF4 CTP5 one to one mapping with existing database

开发者 https://www.devze.com 2023-02-18 03:44 出处:网络
So I have an existing database with a Users table and an Airports table each with a primary key names ID.The Users table has a nullable column DefaultAirportID which is a fk to the ID of the Airports

So I have an existing database with a Users table and an Airports table each with a primary key names ID. The Users table has a nullable column DefaultAirportID which is a fk to the ID of the Airports table.

Here are my POCOs:

public class User : IEntity
{
    [Key]
    [Required]
    public int ID { get; set; }

    public int? DefaultAirportID { get; set; }        
    public Airport DefaultAirpor开发者_如何转开发t { get; set; }

    ...
}

public class Airport : IEntity
{
    [Key]
    [Required]
    public int ID { get; set; }

    ...
}

I keep getting the error "Invalid column name 'AirportID'.". What do I need to do to get it to populate the DefaultAirport object?


You need to use fluent API to configure your association:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>()
                .HasOptional(x => x.DefaultAirport)
                .WithMany()
                .HasForeignKey(n => n.DefaultAirportID);        
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号