开发者

Ef Code first One to one relationship with id as foreign

I'm traying to do a mapping with One to One relationship with id as "foreign", I can't change the database

Those are the tables

Cutomer

  • int CustomerId
  • string Name

CustomerDetail

  • int CustomerId
  • string Details

Entity开发者_运维百科 Splittitng does not works for me since i need a left outter join. Any Ideas?

Thanks in advance, and sorry about my english.


You can use the Shared Primary Key mapping here.

public class Customer
{
    public int CustomerId { get; set; }

    public string Name { get; set; }

    public virtual CustomerDetail CustomerDetail { get; set; }
}

public class CustomerDetail
{
    public int CustomerId { get; set; }

    public string Details { get; set; }

    public virtual Customer Customer { get; set; }
}

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<CustomerDetail>().HasKey(d => d.CustomerId);

        modelBuilder.Entity<Customer>().HasOptional(c => c.CustomerDetail)
            .WithRequired(d => d.Customer);
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜