在Fluent Nhibernate中自动映射复合元素 [英] AutoMapping a Composite Element in Fluent Nhibernate

查看:96
本文介绍了在Fluent Nhibernate中自动映射复合元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取AutoPersistence模型来映射多个复合元素.但是,似乎我要么最终将其映射为一个实体,然后转为手动映射,要么就无法正常工作.这是一些演示我的问题的代码:

I'm trying to get the AutoPersistence model to map several composite elements. However, it seems that either I end up mapping it as an entity, dropping down to manual mapping or it just doesn't plain work. Here's some code that demonstrates my problem:

using System;
using System.Collections.Generic;
using FluentNHibernate.AutoMap;
using FluentNHibernate.Cfg;
using FluentNHibernate.Conventions.Helpers;
using NHibernate.Cfg;

namespace Scanner {
    public class Root {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<Component> Components { get; set; }
    }

    public class Component {
        public string Name { get; set; }
    }

    class Example {
        public void DoesntGetComponents()
        {
            Configuration configuration = new Configuration();
            configuration.SetProperty("ConnectionString", "");
            configuration.SetProperty("dialect", "NHibernate.Dialect.MsSql2005Dialect");
            var config = Fluently.Configure(configuration)
                .Mappings(m => m.AutoMappings.Add(AutoMapping))
                .BuildConfiguration();
            var sql2005 = new NHibernate.Dialect.MsSql2005Dialect();
            foreach (var line in config.GenerateSchemaCreationScript(sql2005))
            {
                Console.WriteLine(line);
            }
        }

        static AutoPersistenceModel AutoMapping() {
            AutoPersistenceModel model = new AutoPersistenceModel();
            return model
                .AddEntityAssembly(typeof(Root).Assembly)
                .WithSetup(e => e.IsComponentType = t => t == typeof(Component))
                .Where(t => t == typeof(Root))
                .MergeWithAutoMapsFromAssemblyOf<Root>()
                .ConventionDiscovery.Add(ForeignKey.Format((p, t) => (p == null ? t.Name : p.Name) + "Id"))
                .ConventionDiscovery.Add(Table.Is(t => t.EntityType.Name))
            ;
        }
    }
}

(很抱歉,它已经很长时间了,但这是演示该问题所需的最少代码.此特定版本的代码根本无法注册组件类型.

(Sorry it's so long, but it's the minimal code required to demonstrate the problem. This particular version of the code fails to register the component type at all.

那么,我在做什么错了?

So, what am I doing wrong?

推荐答案

似乎组件本身不是问题,而是组件集合的映射.如果将组件直接映射到Root类,则不会有任何问题.

It seems that the component in itself is not the problem, but the mapping of a collection of components. If you would map the component directly onto the Root class, this would not be any problem.

一种可能的解决方法是使Component类成为一个实体(添加ID),并覆盖Components到级联+自动删除孤立对象的映射:

A possible workaround is making the Component class an entity (adding an ID) and overriding the mapping of Components to cascade + auto delete orphans:

AutoPersistenceModel
.ForTypesThatDeriveFrom<Root>(map => map.HasMany(root => root.Components).Cascade.AllDeleteOrphan())

这篇关于在Fluent Nhibernate中自动映射复合元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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