2016-07-28 75 views
1

我試圖通過寫一個簡單的主從關係到SQLite數據庫來解決EF7問題。節能工作正常,但是閱讀讓我頭疼:EF7讀取主詳細記錄

這裏是我的實體:

public class Message 
{ 
    public int MessageId { get; set; } 
    public string Name { get; set; }   
    public List<MessagePart> MessageParts { get; set; } 
} 
public class MessagePart 
{ 
    public int MessagePartId { get; set; } 
    public string Text { get; set; }   
    public int MessageId { get; set; } 
    public Message Message { get; set; } 
} 

的CreateMessage()做的事情應該是:

static void createMessages() 
    { 
     using (var db = new TestContext()) 
     { 
      var m1 = new Message 
      { 
       Name = "train_arrives_in_x_minutes",      
       MessageParts = new List<MessagePart>() 
       { 
        new MessagePart { 
         Text = "Train arrives in 5 minutes"        
        }, 
        new MessagePart { 
         Text = "Zug faehrt in 5 Minuten ein",        
        } 
       } 
      }; 
      var m2 = new Message 
      { 
       Name = "train_out_of_service", 
       MessageParts = new List<MessagePart>() 
       { 
        new MessagePart { 
         Text = "train is out of service"        
        }, 
        new MessagePart { 
         Text = "Kein Service auf dieser Strecke",        
        } 
       } 
      }; 

      db.Messages.Add(m1); 
      db.Messages.Add(m2); 

      var count = db.SaveChanges(); 
      Console.WriteLine("{0} records saved to database", count); 

     } 
    } 

從現有的數據庫中讀取讀取主記錄正常,但詳細記錄集指針保持爲空。

static void readMessages() 
    { 
     using (var db = new TestContext()) 
     { 
      foreach (Message m in db.Messages) 
      { 
       Console.WriteLine(m.Name); 
       // exception here: m.MessageParts is always null 
       foreach(MessagePart mp in m.MessageParts) 
       { 
        Console.WriteLine("mp.Text={0}", mp.Text); 
       }      
      }     
     } 
    } 

我能做些什麼來強制這些消息部分加載嗎?我之前和其他(Python)ORM一起工作過,之前從未遇到過這個問題。這是懶惰加載問題嗎?我試圖使用LINQ語句來獲取這些子記錄,但這也沒有幫助。儘管在數據庫中一切看起來不錯。

回答

2

如果要啓用惰性加載您需要啓用惰性加載(應該是默認啓用),讓你的財產虛擬

public TestContext() 
    : base(Name = "ConntextionName") 
{ 
    this.Configuration.ProxyCreationEnabled = true; 
    this.Configuration.LazyLoadingEnabled = true; 
} 

而且你的模型shuodl樣子:

public class Message 
{ 
    public int MessageId { get; set; } 
    public string Name { get; set; }   
    public virtual ICollection<MessagePart> MessageParts { get; set; } 
} 
public class MessagePart 
{ 
    public int MessagePartId { get; set; } 
    public string Text { get; set; }   
    public int MessageId { get; set; } 
    public virtual Message Message { get; set; } 
} 

如果您不想使用LazyLoading,則可以使用預加載加載相關實體:

using System.Data.Entity; 

using (var db = new TestContext()) 
{ 
    int messageId = ....; 
    Message message = db.Messages 
     .Where(m => m.MessageId == messageId) 
     .Include(m => m.MessageParts) // Eagerly load message parts 
     .FirstOrDefault(); 

    // Your message and all related message parts are now loaded and ready. 
} 

欲瞭解更多信息,請看看this site

+1

謝謝,我應該多搜索一下,這裏是另一個:http://ef.readthedocs.io/en/latest/querying/related-data.html – zeroc8