2011-12-02 51 views
0

我遇到了引用問題。 (對象引用未保存的瞬態實例)對象引用未保存的瞬態實例 - 在沖洗前保存瞬態實例

我只是沒有看到問題。任何想法?

所以,這是我想救什麼:


public class ServerInfo : IPersistenzEntity { 
    public virtual int Id { get; private set; } 
    public virtual string Host { get; set; } 
    public virtual IList<ServerUptime> ServerUptime { get; set; } 
} 

public class ServerUptime : IPersistenzEntity { 
    public virtual int Id { get; private set; } 
    // public virtual ServerInfo Server { get; set; } 

    public virtual DateTime StartDate { get; set; } 
    public virtual DateTime EndDate { get; set; } 
} 

這是數據庫:

CREATE TABLE IF NOT EXISTS `server` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `hostname` varchar(255) NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; 


CREATE TABLE IF NOT EXISTS `uptime` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `server_id` int(11) NOT NULL, 
    `startdate` datetime NOT NULL, 
    `enddate` datetime NOT NULL, 
    PRIMARY KEY (`id`), 
    KEY `server_id` (`server_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; 

我使用的映射:

public ServerInfoMap() { 
     Table("server"); 
     // LazyLoad(); 

     Id(x => x.Id); 
     Map(x => x.Host); 

     HasMany(x => x.DriveInfos) 
      .KeyColumn("server_id"); 
     HasMany(x => x.ServerUptime) 
      .KeyColumn("server_id"); 
    } 

    public ServerUptimeMap() { 
     Table("uptime"); 
     // LazyLoad(); 

     Id(x => x.Id); 
     // References(x => x.Server).Column("server_id"); 

     Map(x => x.StartDate); 
     Map(x => x.EndDate); 
    } 

最後的配置:

public void Init(string connectionString) { 
     _sessionFactory = Fluently.Configure() 
      .Database(FluentNHibernate.Cfg.Db.MySQLConfiguration.Standard 
       .ConnectionString(c => c.Is(connectionString))) 
      // .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>()) 
      .Mappings(m => 
        m.FluentMappings 
         .AddFromAssemblyOf<AppliciationSettings>() 
         .Conventions.Add(
          PrimaryKey.Name.Is(x => "id"), 
          DefaultLazy.Always(), 
          ForeignKey.EndsWith("id") 
         ) 
        ) 

      .BuildSessionFactory(); 
    } 


    public void UpdateEntity(IPersistenzEntity entity) { 
     // Initiate update time ... 
     using (var session = Session.OpenSession()) { 
      // populate the database 
      using (var transaction = session.BeginTransaction()) { 
       session.SaveOrUpdate(entity); 
       transaction.Commit(); 
      } 
     } 
    } 

    private void Test() { 
     ServerInfo serverInfo = new ServerInfo() { 
      Host = "host", 
      ServerUptime = new SuperObservableCollection<ServerUptime>(
      ) 
     }; 

     ServerUptime serverUptime = new ServerUptime() { 
      // Server = serverInfo, 
      StartDate = DateTime.Now, 
      EndDate = DateTime.Now, 
     }; 
     serverInfo.ServerUptime.Add(serverUptime); 

     UpdateEntity(serverInfo); 
    } 

回答

2

ServerInfo在他ServerUptime收集了一些新的ServerUptime情況下,其必須被保存到更新參考他們ServerInfo。 USe Cascade.All讓引擎在ServerUptime集合中自動保存新實例

HasMany(x => x.ServerUptime).Cascade.All().KeyColumn("server_id"); 
+0

很好,工作。 :)感謝 – user1075954

+1

如果它工作,你可以接受答案 – Firo

+0

它很好,這個答案的工作,但如果答案說爲什麼要使用Cascade.All來解決這個問題,它會更有幫助。 – Mauro

相關問題