-1
我在Npgsql的對實體框架的核心一個奇怪的行爲遇到不可能的:實體框架的核心越來越Forein主要實體Npgsql的
我的模型:
public class Customer
{
public Customer()
{
Numbers = new List<TelephoneNumber>();
Emails = new List<Email>();
}
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public Address Address { get; set; }
public List<TelephoneNumber> Numbers {get;set;}
public List<Email> Emails { get; set; }
public string Note { get; set; }
}
public class Address
{
public int AddressId { get; set; }
public string Street { get; set; }
public int HouseNumber { get; set; }
public int Code { get; set; }
public string City { get; set; }
public int CustomerId { get; set; }
public Customer Customer { get; set; }
}
public class Email
{
public int EmailId { get; set; }
public string Address { get; set; }
public int CustomerId { get; set; }
public Customer Customer { get; set; }
}
public class TelephoneNumber
{
public int TelephoneNumberId { get; set; }
public string Number { get; set; }
public int CustomerId { get; set; }
public Customer Customer { get; set; }
}
而且我的DbContext:
public class CustomerContext : DbContext
{
public CustomerContext(DbContextOptions<CustomerContext> options) : base(options)
{}
public DbSet<Customer> Customers {get; set;}
public DbSet<Address> Addresses { get; set; }
public DbSet<Email> Emails {get;set;}
public DbSet<TelephoneNumber> Numbers {get;set;}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Customer>().HasKey(m => m.CustomerId);
builder.Entity<Address>().HasKey(m => m.AddressId);
builder.Entity<Email>().HasKey(m => m.EmailId);
builder.Entity<TelephoneNumber>().HasKey(m => m.TelephoneNumberId);
base.OnModelCreating(builder);
}
}
我已成功連接到postgresql數據庫,並且模式是使用「dotnet ef migrations add initPG」和「dotnet ef database update」創建的,沒有righ問題牛逼forein鍵約束,等等。
然後創建一個客戶:
var created = _context.Customers.Add(cust);
_context.SaveChanges();
以客戶包含地址,電子郵件和電話號碼。
我的問題是,當我想從數據庫中的所有客戶:
IEnumerable<Customer> customers = _context.Customers.ToList();
我只得到了客戶與空地址,電子郵件和TelephoneNumber屬性!
我在這裏有一個合理的誤解!?我認爲EF-Framework會自動加入表格。 還是我手動需要從我的客戶得到這個單獨的元素?
在幾個來源,我看到包括關鍵字來獲取forein鍵的屬性,但我的IDE表明的DbContext沒有包括方法了!
是否有必要使用include來包含所有外鍵屬性? – rubiktubik
@rubiktubik我似乎記得有一種方法來配置該行爲,但它會降低性能。如果你必須知道,[谷歌搜索讓我到這個問題](http://stackoverflow.com/questions/2967214/disable-lazy-loading-by-default-in-entity-framework-4) –
它似乎是延遲加載尚未在ef7中實現,因此需要使用包含方法 – rubiktubik