简体   繁体   English

流利的NHibernate和自动映射的关系问题

[英]Relation problems with Fluent NHibernate and Automappings

I have made a simple example application to test Fluent NHibernate with the automapping feature but I get an exception when I save. 我已经制作了一个简单的示例应用程序来测试具有自动映射功能的Fluent NHibernate,但保存时出现异常。

This is how my example looks like: 这是我的示例如下所示:

public class Item  {
    public virtual int Id { get; protected set; }
    public virtual string Name { get; set; }
    public virtual IList<Detail> Details { get; set; }
    public Item() {
        Details = new List<Detail>();
    }
}
public class Detail {
    public virtual int Id { get; protected set; }
    public virtual int ItemId { get; set; }
    public virtual string Name { get; set; }
}
class Program {
    static void Main(string[] args) {
        var sessionFactory = CreateSessionFactory();
        using (var session = sessionFactory.OpenSession()) {
            using (var transaction = session.BeginTransaction()) {

                Console.WriteLine("Enter name for new item: ");
                var itemName = Console.ReadLine();
                var page = new Item { Name = itemName };
                Console.WriteLine("Enter name of new detail");
                var detailName = Console.ReadLine();
                var detail = new Detail {Name = detailName };
                page.Details.Add(detail);
                session.SaveOrUpdate(page);
                transaction.Commit();
            }
            using (session.BeginTransaction()) {
            }
            Console.ReadKey();
        }

    }
    private static ISessionFactory CreateSessionFactory() {

        var model = AutoMap.AssemblyOf<Program>();

        var sessionFactory = Fluently.Configure()
          .Database(MsSqlConfiguration.MsSql2008.ConnectionString(connection)
          .Cache(c => c.UseQueryCache()
            .ProviderClass<HashtableCacheProvider>())
            .ShowSql())
          .Mappings(m => m.AutoMappings.Add(model))
          .BuildSessionFactory();

        return sessionFactory;
    }
}

This code throws an exception when transaction.Commit() is running and the exception is object references an unsaved transient instance - save the transient instance before flushing. 当transaction.Commit()正在运行并且该对象引用了未保存的临时实例时,此代码将引发异常-在刷新之前保存该临时实例。

So can anybody help me figure out what's wrong? 那么有人可以帮助我找出问题所在吗?

Your detail instance is "transient", which means that is has not been added to your NHibernate session. 您的detail实例是“瞬态的”,这意味着尚未将其添加到您的NHibernate会话中。

You have two option, where #2 is probably preferred in your case: 您有两种选择,其中#2可能是您的首选:

1) session.Save(detail) before you flush the session 1) session.Save(detail)在刷新会话之前

2) Configure your Details collection to cascade 2)配置您的Details集合以进行级联

There are many ways to configure cascading when you are using the automapping feature - I usually prefer to "pollute" my domain model with [Cascade] attributes, and then create a simple implementation of IHasManyConvention and IReferenceConvention that checks for the presence of the attribute and sets the cascading option accordingly. 使用自动映射功能时,有许多方法可以配置级联-我通常更喜欢用[Cascade]属性“污染”我的域模型,然后创建一个IHasManyConventionIReferenceConvention的简单实现,以检查该属性是否存在以及相应地设置级联选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM