2016-08-11 263 views
1

我試圖關係添加到網頁API項目中使用EF代碼第一次我的實體實體框架代碼優先 - 關係

我的數據庫種子碼是

internal class ProductDbInitializer : DropCreateDatabaseAlways<ProductDbContext> 
{ 
    protected override void Seed(ProductDbContext context) 
    { 
     Category category = new Category(); 
     LanguagesDbContext languagesDbContext = new LanguagesDbContext(); 
     List<Language> languages = new List<Language> 
     { 
      new Language {Name="English",Code="en" }, 
      new Language {Name="Italian", Code="it" }, 
      new Language {Name="Russian",Code="rus" }, 
     }; 

     languagesDbContext.Languages.AddRange(languages); 

     context.Categories.Add(category); 

     List<CategoryText> categoryText = new List<CategoryText>(); 

     foreach (var language in languages) 
     { 
      string name = string.Empty; 

      switch (language.Code) 
      { 
       case "en": 
        name = "paper"; 
        break; 
       case "it": 
        name = "carta"; 
        break; 
       case "rus": 
        name = "бумага"; 
        break; 
       default: 
        name = "paper"; 
        break; 
      } 

      categoryText.Add(new CategoryText 
      { 
       Category = category, 
       Language = language, 
       Name = name 
      }); 
     } 

     category.CategoryTexts.Add(categoryText[1]); 
     context.CategoryTexts.AddRange(categoryText); 

     context.SaveChanges(); 
    } 
} 

一切都存儲在數據庫,我想在我的課的申報問題,因爲當我想Categories我沒有能夠看到CategoryTexts

這是瀏覽器輸出

output in browser

但在數據庫中,我的CategoryTexts已添加有效的CategoryId

這是我的類別和CategoryText實體

public class Category 
{ 
    public Category() 
    { 
     CategoryTexts = new List<CategoryText>(); 
    } 

    public int Id { get; set; } 
    public ICollection<Product> Products { get; set; } 
    public ICollection<CategoryText> CategoryTexts { get; set; } 
} 

public class CategoryText 
{ 
    public int Id { get; set; } 
    public int CategoryId { get; set; } 
    public int LanguageId { get; set; } 

    [ForeignKey("CategoryId")] 
    public Category Category { get; set; } 

    [ForeignKey("LanguageId")] 
    public Language Language { get; set; } 
    public string Name { get; set; } 
} 

請幫助我理解我在哪裏需要更改代碼

回答

2

你需要或者渴望負荷你的人際關係(當您查詢時使用Include),例如:

var myList = context.Products.Include("Categories").Include("CategoryTexts").ToList(); 

或者你可以明確地加載它們(與context.Entry(x).[Reference|Collection](x).Load(),例如:

var myProduct = context.Products.First(); 
context.Entry(myProduct).Collection(x => x.Categories).Load(); 

或者:

var myCategoryText = context.CategoryText.First(); 
context.Entry(myCategoryText).Reference(x => x.Language).Load(); 

或者你可以讓他們使用代理的自動負載(延遲加載)...但爲此,您的外國實體和收藏必須在您的POCO中標記爲virtual

public class Category 
{ 
    public Category() 
    { 
     CategoryTexts = new List<CategoryText>(); 
    } 

    public int Id { get; set; } 
    public virtual ICollection<Product> Products { get; set; } 
    public virtual ICollection<CategoryText> CategoryTexts { get; set; } 
} 

public class CategoryText 
{ 
    public int Id { get; set; } 
    public int CategoryId { get; set; } 
    public int LanguageId { get; set; } 

    [ForeignKey("CategoryId")] 
    public virtual Category Category { get; set; } 

    [ForeignKey("LanguageId")] 
    public virtual Language Language { get; set; } 
    public string Name { get; set; } 
} 

你還需要有代理的創建和延遲加載開啓,但這是在EF6默認。

還記得那方面必須是「活的」(未配置)accesing屬性時,這樣有一個活的情況下,他們可以被加載

鏈接到相關的文檔:https://msdn.microsoft.com/en-us/data/jj574232.aspx

+0

所以如果我標記作爲虛擬它會自動添加? –

+0

是的,如果你有延遲加載和代理生成,並且在訪問引用時上下文仍然可用(不處理),它會加載它 – Jcl

+0

非常感謝你的信息,你是英雄 –