流利的NHibernate DuplicateMappingException与AutoMapping [英] Fluent NHibernate DuplicateMappingException with AutoMapping

查看:136
本文介绍了流利的NHibernate DuplicateMappingException与AutoMapping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Fluent NHibernate Automapper保存两个相同名称和不同名称空间的类。

b
$ b

上下文

我正在编写必须将大量不同的对象导入数据库进行测试。我最终将映射器写入一个合适的模型。



我一直使用代码gen和Fluent NHibernate来把这些DTOs直接转储到db。



异常确实会说(尝试使用auto-import =false)

代码

  public class ClassConvention:IClassConvention 
{
public void Apply IClassInstance实例)
{
instance.Table(instance.EntityType.Namespace.Replace(。,_));



命名空间Sample.Models.Test1
{
public class Test
{
public virtual int Id {得到;组; }
public virtual string Something {get;组; }



命名空间Sample.Models.Test2
{
public class Test
{
public virtual int Id {get;组; }
公共虚拟字符串SomethingElse {get;组; }




$ b $ p $这里是实际的应用程序代码


$ b $

  var model = AutoMap.AssemblyOf< Service1>()
.Where(t => t.Namespace.StartsWith(Sample .Models))
.Conventions.AddFromAssemblyOf< Service1>();
var cfg =流利的.Configure()
。数据库(
MySQLConfiguration.Standard.ConnectionString(
c => c.Is(database = test; server = localhost; user id = root; Password =;)))
.Mappings(m => m.AutoMappings.Add(model))
.BuildConfiguration();
新的SchemaExport(cfg).Execute(false,true,false);

非常感谢任何帮助

更新使用Fluent Nhibernate RC1

解决方案

来自fluent-nhibernate论坛的解决方案 James Gregory
$ b


今晚有一个适当的看看
。基本上,AutoImport是
提到的异常;当NHibernate被赋予
第一个映射时,它看到
实体被命名为完整程序集
限定名,并为短名称创建一个导入
(有帮助!),
,然后当你添加第二个
时,它会抱怨这个输入是
现在会冲突。所以解决方案
是关闭自动导入;
不幸的是,我们没有办法在$ RC中做
...我只是
提交了一个修正,增加了
的能力来改变这个一个
惯例。所以,如果你得到最新的
二进制文件或源代码,你应该能够
来改变你的附加项目
中的约定行:



  .Conventions.Setup(x => {
x.AddFromAssemblyOf< Program>();
x。 Add(AutoImport.Never());});




其中添加了
中定义的所有约定你的程序集,然后使用
辅助约定中的一个将
关闭自动导入。



Summary:

I want to save two classes of the same name and different namespaces with the Fluent NHibernate Automapper

Context

I'm writing having to import a lot of different objects to database for testing. I'll eventually write mappers to a proper model.

I've been using code gen and Fluent NHibernate to take these DTOs and dump them straight to db.

the exception does say to (try using auto-import="false")

Code

public class ClassConvention : IClassConvention
{
    public void Apply(IClassInstance instance)
    {
        instance.Table(instance.EntityType.Namespace.Replace(".", "_"));
    }
}

namespace Sample.Models.Test1
{
    public class Test
    {
        public virtual int Id { get; set; }
        public virtual string Something { get; set; }
    }
}

namespace Sample.Models.Test2
{
    public class Test
    {
        public virtual int Id { get; set; }
        public virtual string SomethingElse { get; set; }        
    }
}

And here's the actual app code

            var model = AutoMap.AssemblyOf<Service1>()
                .Where(t => t.Namespace.StartsWith("Sample.Models"))
                .Conventions.AddFromAssemblyOf<Service1>();
            var cfg = Fluently.Configure()
                .Database(
                MySQLConfiguration.Standard.ConnectionString(
                    c => c.Is("database=test;server=localhost;user id=root;Password=;")))
                .Mappings(m => m.AutoMappings.Add(model))
                .BuildConfiguration();
            new SchemaExport(cfg).Execute(false, true, false);

Thanks I really appreciate any help

Update using Fluent Nhibernate RC1

解决方案

solution from fluent-nhibernate forums by James Gregory

Got around to having a proper look at this tonight. Basically, it is down to the AutoImport stuff the exception mentioned; when NHibernate is given the first mapping it sees that the entity is named with the full assembly qualified name and creates an import for the short name (being helpful!), and then when you add the second one it then complains that this import is now going to conflict. So the solution is to turn off the auto importing; unfortunately, we don't have a way to do that in the RC... I've just commited a fix that adds in the ability to change this in a convention. So if you get the latest binaries or source, you should be able to change your Conventions line in your attached project to do this:

.Conventions.Setup(x =>  {   
  x.AddFromAssemblyOf<Program>();   
  x.Add(AutoImport.Never());  }); 

Which adds all the conventions you've defined in your assembly, then uses one of the helper conventions to turn off auto importing.

这篇关于流利的NHibernate DuplicateMappingException与AutoMapping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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