级联保存与功能NHibernate自动映射 - 老回答是否仍然有效? [英] Cascade Saves with Fluent NHibernate AutoMapping - Old Answer Still Valid?

查看:379
本文介绍了级联保存与功能NHibernate自动映射 - 老回答是否仍然有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的正是这个问题问:
的http:/ /stackoverflow.com/questions/586888/cascade-saves-with-fluent-nhibernate-automapping



使用流利的NHibernate的映射打开层叠全球一旦用一个电话,而不是单独设置它为每个映射所有类和关系类型。



答案前面的问题看起来很大,但我怕流利NHibernate的API改变了其.WithConvention语法去年打破了答案...要么还是我失去了一些东西。



我不断收到一堆名字空间不是发现错误有关的IOneToOnePart,IManyToOnePart和他们所有的变化:



的类型或命名空间名称IOneToOnePart'找不到(是否缺少using指令或程序集引用?)



我试过官方例如dll的,在RTM DLL的和最新的构建和他们都不做VS 2008查看所需的命名空间。



第二个问题是,我想使用这个类我AutoPersistenceModel
,但我不知道在哪里这一行:
.ConventionDiscovery.AddFromAssemblyOf()
在我厂创建方法。

 私有静态ISessionFactory CreateSessionFactory()
{

返回Fluently.Configure()
.Database(SQLiteConfiguration.Standard.UsingFile(DBFILE))
.Mappings(M = GT; m.AutoMappings
。新增(AutoMap.AssemblyOf<保质>(类型=> type.Namespace .EndsWith(实体))
.Override<保质>(地图= GT;
{
map.HasManyToMany(X => x.Products).Cascade.All();
})


)// EMD映射
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory(); //最后确定整个事情发背部。

}



下面是类和使用语句我试图

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
:使用System.IO;
$ B $使用FluentNHibernate.Conventions B:
使用FluentNHibernate.Cfg;
使用FluentNHibernate.Cfg.Db;
使用NHibernate;
使用NHibernate.Cfg;
使用NHibernate.Tool.hbm2ddl;
使用FluentNHibernate.Mapping;


命名空间TestCode
{
公共类CascadeAll:IHasOneConvention,IHasManyConvention,IReferenceConvention
{
公共BOOL接受(IOneToOnePart目标)
{
返回真;
}

公共无效申请(IOneToOnePart目标)
{
target.Cascade.All();
}

公共BOOL接受(IOneToManyPart目标)
{
返回真;
}

公共无效申请(IOneToManyPart目标)
{
target.Cascade.All();
}

公共BOOL接受(IManyToOnePart目标)
{
返回真;
}

公共无效申请(IManyToOnePart目标)
{
target.Cascade.All();
}
}

}


解决方案

我发现了整个项目最简单的方法是使用 DefaultCascade

  .Conventions.Add(DefaultCascade.All()); 



最简单的约定在wiki部分,对于这一点,和其他人的名单。



编辑:
下面是来自维基名单:

  Table.Is(X => x.EntityType.Name +表)
PrimaryKey.Name.Is(X =>中ID)
AUTOIMPORT .Never()
DefaultAccess.Field()
DefaultCascade.All()
DefaultLazy.Always()
DynamicInsert.AlwaysTrue()
DynamicUpdate.AlwaysTrue()
OptimisticLock.Is(X => x.Dirty())
Cache.Is(X => x.AsReadOnly())
ForeignKey.EndsWith(ID)

警告词 - 有的在维基的方法名的可能是错误的。我编辑的维基什么我可以验证(即DefaultCascade和DefaultLazy),但无法保证休息。但是,你应该能够找出适当的名称与智能感知,如有需要。


I want to do exactly what this question asks: http://stackoverflow.com/questions/586888/cascade-saves-with-fluent-nhibernate-automapping

Using Fluent Nhibernate Mappings to turn on "cascade" globally once for all classes and relation types using one call rather than setting it for each mapping individually.

The answer to the earlier question looks great, but I'm afraid that the Fluent Nhibernate API altered its .WithConvention syntax last year and broke the answer... either that or I'm missing something.

I keep getting a bunch of name space not found errors relating to the IOneToOnePart, IManyToOnePart and all their variations:

"The type or namespace name 'IOneToOnePart' could not be found (are you missing a using directive or an assembly reference?)"

I've tried the official example dll's, the RTM dll's and the latest build and none of them seem to make VS 2008 see the required namespace.

The second problem is that I want to use the class with my AutoPersistenceModel but I'm not sure where to this line: .ConventionDiscovery.AddFromAssemblyOf() in my factory creation method.

 private static ISessionFactory CreateSessionFactory()
            {

                return Fluently.Configure()
                  .Database(SQLiteConfiguration.Standard.UsingFile(DbFile))
                  .Mappings(m => m.AutoMappings
                        .Add(AutoMap.AssemblyOf<Shelf>(type => type.Namespace.EndsWith("Entities"))
                                .Override<Shelf>(map =>
                                {
                                    map.HasManyToMany(x => x.Products).Cascade.All();
                                })
                             )

                      )//emd mappings
                .ExposeConfiguration(BuildSchema)
                .BuildSessionFactory();//finalizes the whole thing to send back. 

            }

Below is the class and using statements I'm trying

   using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;

    using FluentNHibernate.Conventions;
    using FluentNHibernate.Cfg;
    using FluentNHibernate.Cfg.Db;
    using NHibernate;
    using NHibernate.Cfg;
    using NHibernate.Tool.hbm2ddl;
    using FluentNHibernate.Mapping;


    namespace TestCode
    {
        public class CascadeAll : IHasOneConvention, IHasManyConvention, IReferenceConvention
        {
            public bool Accept(IOneToOnePart target)
            {
                return true;
            }

            public void Apply(IOneToOnePart target)
            {
                target.Cascade.All();
            }

            public bool Accept(IOneToManyPart target)
            {
                return true;
            }

            public void Apply(IOneToManyPart target)
            {
                target.Cascade.All();
            }

            public bool Accept(IManyToOnePart target)
            {
                return true;
            }

            public void Apply(IManyToOnePart target)
            {
                target.Cascade.All();
            }
        }

    }

解决方案

The easiest way I've found to do this for a whole project is to use DefaultCascade:

.Conventions.Add( DefaultCascade.All() );     

Go to "The Simplest Conventions" section on the wiki, for this, and a list of others.

Edit: Here's the list from the Wiki:

Table.Is(x => x.EntityType.Name + "Table")
PrimaryKey.Name.Is(x => "ID")
AutoImport.Never()
DefaultAccess.Field()
DefaultCascade.All()
DefaultLazy.Always()
DynamicInsert.AlwaysTrue()
DynamicUpdate.AlwaysTrue()
OptimisticLock.Is(x => x.Dirty())
Cache.Is(x => x.AsReadOnly())
ForeignKey.EndsWith("ID")

A word of warning - some of the method names in the Wiki may be wrong. I edited the Wiki with what I could verify (i.e. DefaultCascade and DefaultLazy), but can't vouch for the rest. But you should be able to figure out the proper names with Intellisense if the need arises.

这篇关于级联保存与功能NHibernate自动映射 - 老回答是否仍然有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆