object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例

时间:2011-12-02 15:50:38

标签: c# fluent-nhibernate

我的引用存在问题。 (对象引用未保存的瞬态实例)

我只是没有看到问题。有什么想法吗?

所以,这就是我想要保存的内容:


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);
    }

1 个答案:

答案 0 :(得分:2)

ServerInfo在其ServerUptime集合中有一些新的ServerUptime个实例,必须保存这些实例以更新其中ServerInfo的引用。使用Cascade.All让引擎自动在ServerUptime集合中保存新实例

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