2011-12-02 50 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

相关问题