Ef Code first Отношение один к одному с id как иностранным - PullRequest
0 голосов
/ 27 сентября 2011

Я пытаюсь сопоставить отношения «один к одному» с идентификатором «чужой», я не могу изменить базу данных

Это таблицы

Cutomer

  • int CustomerId
  • Имя строки

CustomerDetail

  • int CustomerId
  • строка Подробности

Entity Splittitng не работает для меня, так как мне нужно левое внешнее соединение. Есть идеи?

Заранее спасибо, и извините за мой английский.

1 Ответ

1 голос
/ 27 сентября 2011

Здесь вы можете использовать отображение общего первичного ключа.

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);
    }
}
...