问题描述
我正在尝试使用流利的自动地图功能来生成用于以下模型和程序的DDL,但我一直不知何故当我调用时出现错误关联引用未映射的类:IRole"NHibernate中的GenerateSchemaCreationScript方法.当我更换接口实现的ILists的类型(用户和角色),一切正常.我在这里做错了什么?我怎样才能流利地使用已定义的IUser和IRole的实现版本在Unity中?
I'm trying to use the automap functionality in fluent to generate aDDL for the following model and program, but somehow I keep gettingthe error "Association references unmapped class: IRole" when I callthe GenerateSchemaCreationScript method in NHibernate. When I replacethe type of the ILists with the implementation of the interfaces (Userand Role) everything works fine. What am I doing wrong here? How can Imake fluent use the implemented versions of IUser and IRole as definedin 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 theGenerateSchemaCreationScript 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 .下面是您需要的一个粗略示例.
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
这篇关于得到错误“关联引用未映射的类".在模型中使用接口时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!