gpt4 book ai didi

mysql - 带有 AutoMapping 的 Fluent NHibernate DuplicateMappingException

转载 作者:可可西里 更新时间:2023-11-01 07:13:11 26 4
gpt4 key购买 nike

总结:

我想用 Fluent NHibernate Automapper 保存两个同名不同命名空间的类

上下文

我写的是必须将大量不同的对象导入数据库进行测试。我最终会将映射器写入适当的模型。

我一直在使用 code gen 和 Fluent NHibernate 获取这些 DTO 并将它们直接转储到 db。

异常会提示(尝试使用 auto-import="false")

代码

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

这是实际的应用程序代码

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

非常感谢任何帮助

更新使用 Fluent Nhibernate RC1

最佳答案

solution from fluent-nhibernate forums通过詹姆斯格雷戈里

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.

关于mysql - 带有 AutoMapping 的 Fluent NHibernate DuplicateMappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1290466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com