2013-04-22 62 views
0

我有,並通過數據服務公開實體框架項目:不能正確添加相關對象到實體框架上下文

public class VersionContext : DbContext 
{ 
    public DbSet<VersionTreeEntry> VersionTreeEntries { get; set; } 

    public DbSet<PluginState> PluginStates { get; set; } 

    public static void SetForUpdates() 
    { 
     Database.SetInitializer(new MigrateDatabaseToLatestVersion<VersionContext, Configuration>()); 
    } 
} 

public class VersionTreeEntry 
{ 
    public VersionTreeEntry() 
    { 
     Children = new List<VersionTreeEntry>(); 
     PluginStates = new List<PluginState>(); 
    } 

    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public long Id { get; set; } 

    public virtual ICollection<VersionTreeEntry> Children { get; set; } 

    public virtual ICollection<PluginState> PluginStates { get; set; } 

    public virtual VersionTreeEntry Ancestor { get; set; } 

    /// <summary> 
    /// Links to the ProtoBufDataItem Id for the session state. 
    /// </summary> 
    public int DataId { get; set; } 

    public string Notes { get; set; } 

    [Required] 
    public DateTime TimeStamp { get; set; } 

    [MinLength(1, ErrorMessage = "Tag cannot have a zero length")] 
    [MaxLength(20, ErrorMessage = "A tag name cannot contain over 20 characters")] 
    public string Tag { get; set; } 

    public bool IsUiNodeExpanded { get; set; } 

    [Required] 
    public string Version { get; set; } 

    [Required] 
    public string SessionName { get; set; } 
} 

public class PluginState 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public long Id { get; set; } 

    public string PluginName { get; set; } 

    [Required] 
    public byte[] Data { get; set; } 
} 

據我所看到的,數據類正確定義。我試圖創造一些新的對象,並將其添加到環境中,他們的關係不變:

var session = new Session(); 
session.SessionName = "My new session"; 

VersionTreeEntry versionTreeEntry = new VersionTreeEntry(); 
versionTreeEntry.SessionName = session.SessionName; 
versionTreeEntry.Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); 
versionTreeEntry.TimeStamp = DateTime.Now; 

_versionContext.AddToVersionTreeEntries(versionTreeEntry); 

foreach (var plugin in session.Plugins) 
{ 
    using (var ms = new MemoryStream()) 
    { 
      plugin.SaveState(ms); 
      PluginState state = new PluginState(); 
      state.PluginName = plugin.PluginName; 
      state.Data = ms.ToArray(); 

      versionTreeEntry.PluginStates.Add(state); 
    } 
} 

_versionContext.SaveChanges(); 

的問題是,PluginState情況下,實際上從未被添加到數據庫中。如果我添加代碼以手動將它們添加到上下文,則它們會添加,但指向VersionTreeEntry的外鍵爲空。

同樣,這是一個WCF DataService而不是vanilla EF,任何想法可能是什麼錯誤?

乾杯

+0

可以將下面的行添加到您的代碼在for循環調用後添加方法 - _versionContext.AddLink(versionTreeEntry ,「PluginStates」,狀態)。希望這可以幫助。 – Pratik 2013-04-22 18:18:54

+0

感謝Pratik。聽起來像是對我的回答:) 我也可以用_versionContext.AddRelatedObject(versionTreeEntry,「PluginStates」,state)替換兩行。 – Avram 2013-04-23 08:45:22

回答

1

在評論部分發布答案。

同意。要做到這一點,最好的方法是調用以下API:

_versionContext.AddRelatedObject(versionTreeEntry, "PluginStates", state); 

感謝 PRATIK

相關問題