得到错误“关联引用未映射的类".在模型中使用接口时 [英] Getting error "Association references unmapped class" when using interfaces in model

查看:176
本文介绍了得到错误“关联引用未映射的类".在模型中使用接口时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用流利的自动地图功能来生成 用于以下模型和程序的DDL,但我一直不知何故 当我调用时出现错误关联引用未映射的类:IRole" NHibernate中的GenerateSchemaCreationScript方法.当我更换 接口实现的ILists的类型(用户 和角色),一切正常.我在这里做错了什么?我怎样才能 流利地使用已定义的IUser和IRole的实现版本 在Unity中?

I'm trying to use the automap functionality in fluent to generate a DDL for the following model and program, but somehow I keep getting the error "Association references unmapped class: IRole" when I call the GenerateSchemaCreationScript method in NHibernate. When I replace the type of the ILists with the implementation of the interfaces (User and Role) everything works fine. What am I doing wrong here? How can I make fluent use the implemented versions of IUser and IRole as defined in Unity?

public interface IRole
{
   string Title { get; set; }
   IList<IUser> Users { get; set; }
}

   public interface IUser
   {
       string Email { get; set; }
       IList<IRole> Roles { get; set; }
   }

public class Role : IRole
{
   public virtual string Title { get; set; }
   public virtual IList<IUser> Users { get; set; }
}
public class User : IUser
{
   public virtual string Email { get; set; }
   public virtual IList<IRole> Roles { get; set; }
}

我使用以下程序使用 NHibernate中的GenerateSchemaCreationScript:

I use the following program to generate the DDL using the GenerateSchemaCreationScript in NHibernate:

class Program
{
   static void Main(string[] args)
   {
       var ddl = new NHibernateSessionManager();
       ddl.BuildConfiguration();
   }
}

   public class NHibernateSessionManager
   {
       private ISessionFactory _sessionFactory;
       private static IUnityContainer _container;

       private static void InitContainer()
       {
           _container = new UnityContainer();
           _container.RegisterType(typeof(IUser), typeof(User));
           _container.RegisterType(typeof(IRole), typeof(Role));
       }

       public ISessionFactory BuildConfiguration()
       {
           InitContainer();
           return
Fluently.Configure().Database(MsSqlConfiguration.MsSql2008
                .ConnectionString("ConnectionString"))
                .Mappings(m => m.AutoMappings.Add(
                    AutoMap.AssemblyOf<IUser>()))
                .ExposeConfiguration(BuildSchema)
                .BuildSessionFactory();
       }

       private void BuildSchema(Configuration cfg)
       {
           var ddl = cfg.GenerateSchemaCreationScript(new
NHibernate.Dialect.MsSql2008Dialect());
           System.IO.File.WriteAllLines("Filename", ddl);
       }

   }

推荐答案

我和您的处境相同.在我知道可以使用Fluent进行操作之前使用过ClassMap,但是我以前从未使用过自动映射功能.我已经能够成功地使用IReferenceConvention与AutoMapper进行一对一映射(请参见前面的发布).

I am in the same situation as you. Having used the ClassMap before I know you can do this with Fluent but I had never used the AutoMapping feature before. I have successfully been able to do a one to one mapping with the AutoMapper using an IReferenceConvention (see previous SO post).

我现在遇到了与您相同的问题,因为我有一对多映射,现在遇到了问题.我已经开始研究一个IHasManyConvention界面,但是到目前为止还没有运气.

I have now hit the same problem as you where I have a one to many mapping which I am now having a problem with. There is an IHasManyConvention interface which I have started to look at but have had no luck as of yet.

仅仅因为有些事情很难做就不会出错,对接口的映射绝对有价值,可以在原始的nHibernate映射文件中或通过使用Fluents ClassMap映射文件轻松完成.我认为一旦人们开始使用自动映射"功能做更多的事情,就会有更多的博客文章.

Just because some thing is hard to do it doesn't make it wrong, mapping to interfaces defiantly has value and can easily be done in the raw nHibernate mapping files or by using Fluents ClassMap mapping files. I think once people start do more with AutoMapping feature there will be more blog posts.

编辑

我找到了一个使用 IAutoMappingOverride <的临时解决方案/a>.下面是您需要的一个粗略示例.

I have found an interim solution using an IAutoMappingOverride. Below is a rough example of what you need.

public class RoleAutoMappingOverride : IAutoMappingOverride<Role>
{
    public void Override(AutoMapping<Role> mapping)
    {
        mapping.HasMany<User>( x => x.Users ).KeyColumn( "User_id" );
    }
}

编辑

我的一所大学找到了一种更好的解决方案,该方法使用约定而不是替代.这涵盖了如何做一个单一的类,但是如果您看一下我之前提到的SO帖子,您将看到如何使它通用.

A college of mine has worked out a better solution that uses conventions instead of the override. This covers how to do a single class but if you look at the SO post I mentioned before you can see how this could be made generic.

public class Foo : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        if (instance.ChildType == typeof(Role))
        {
            instance.Relationship.CustomClass<User>();
        }
    }
}

编辑

我现在已经将此内容和其他文章变成了博客文章: http://bronumski.blogspot.com/2011/01/Making-fluent-nhibernate-automapper.html

I have now turned this and my other post into a blog post: http://bronumski.blogspot.com/2011/01/making-fluent-nhibernate-automapper.html

这篇关于得到错误“关联引用未映射的类".在模型中使用接口时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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