如何使用 Fluent NHibernate 映射 aspnet_Users 表的一部分
我试图让 Fluent NHibernate 1.0 RTM 为我映射一个 User 实体,以便我可以通过 NHibernate 访问 ASP.NET MVC 应用程序中的 UserId 和 UserName。
我有:
public class User
{
public virtual Guid UserId { get; protected set; }
public virtual string UserName { get; protected set; }
}
它代表 aspnet_Users 表,仅包含要映射的相关列。这是唯一没有被自动映射的实体。这是我的映射:
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.UserId);
Map(x => x.UserName);
WithTable("aspnet_Users");
}
}
其他所有内容都按照约定自动映射。
以下是我的 PrimaryKeyConvention 和 TableNameConvention:
public class PrimaryKeyConvention : IIdConvention
{
public void Apply(IIdentityInstance instance)
{
instance.Column(instance.Property.ReflectedType.Name + "Id");
instance.UnsavedValue(System.Guid.Empty.ToString());
instance.GeneratedBy.GuidComb();
}
}
public class TableNameConvention : IClassConvention
{
public void Apply(IClassInstance instance)
{
instance.Table(Inflector.Net.Inflector.Pluralize(instance.EntityType.Name));
}
}
在执行 ClassMap 代码(在所有自动映射之前)、随后是 TableNameConvention 代码和 PrimaryKeyConvention 代码后,映射过程立即失败。失败发生在 PrimaryKeyConvention 中,因为 instance.Property 为 null。我尝试做一个 if(instance.Property != null) 但那 提前终止映射过程并出现“缺少所需属性‘类’”错误。我在 TableNameConvention 中也有一个 if (instance.EntityType != typeof(User)) ,但是当它没有任何区别时就将其删除。
这是怎么回事?首先,为什么 AutoMapping 进程要调用 ClassMap 的约定?其次,为什么 PrimaryKenConvention 会传递一个 instance.Property == null ?我怎样才能让它工作,以便映射过程继续进行并使用 AutoMapping + 约定映射我的其余实体?
请注意,在 1.0 RTM 重构之前,我已经在早期版本的 FNH 下让这一切工作了几个月。
I'm trying to get Fluent NHibernate 1.0 RTM to map a User entity for me so that I have access to UserId and UserName inside my ASP.NET MVC application via NHibernate.
I have:
public class User
{
public virtual Guid UserId { get; protected set; }
public virtual string UserName { get; protected set; }
}
It represents the aspnet_Users table with only the relevant columns to be mapped. This is the only entity that is not being automapped. Here is my mapping:
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.UserId);
Map(x => x.UserName);
WithTable("aspnet_Users");
}
}
Everything else is getting automapped with conventions.
Here are my PrimaryKeyConvention and TableNameConvention:
public class PrimaryKeyConvention : IIdConvention
{
public void Apply(IIdentityInstance instance)
{
instance.Column(instance.Property.ReflectedType.Name + "Id");
instance.UnsavedValue(System.Guid.Empty.ToString());
instance.GeneratedBy.GuidComb();
}
}
public class TableNameConvention : IClassConvention
{
public void Apply(IClassInstance instance)
{
instance.Table(Inflector.Net.Inflector.Pluralize(instance.EntityType.Name));
}
}
The Mapping process fails right after executing the ClassMap code (which comes before all automapping), followed by the TableNameConvention code, followed by the PrimaryKeyConvention code. The failure is in PrimaryKeyConvention because instance.Property is null. I tried to do an if(instance.Property != null) but that
terminates the mapping process early with a "the required attribute 'class' is missing" error. I also had an if (instance.EntityType != typeof(User)) in the TableNameConvention, but took out when it was making no difference.
What is going on here? First of all, why is the AutoMapping processes calling the conventions for the ClassMap? Second, why is the PrimaryKenConvention getting passed an instance.Property == null? How can I get this to work so that the mapping process moves on and maps the rest of my entities using AutoMapping + conventions?
Note, I had this all working for months under an earlier version of FNH prior to the refactor for 1.0 RTM.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经找到了这个问题的答案。
请注意,PrimaryKeyConvention 使用instance.EntityName 而不是instance.Property 设置列名称。后者对于 UserMap 来说是空的,所以它会崩溃。
这种方法比在 (null != instance.Property) 上使用条件语句并保留 instance.Property.ReflectedType.Name 行更好,但两者都有效。如果您选择走这条路线,则必须在 UserMap 中显式设置列名称:
I've figured out the answer to this.
Note that the PrimaryKeyConvention sets the Column name using instance.EntityName rather that instance.Property. The latter was null for the UserMap, so it would crash.
This approach is better than using a conditional statement on (null != instance.Property) and keeping the instance.Property.ReflectedType.Name line, but both work. If you choose to go that route, you have to explicitly set the Column names in the UserMap:
我自己不使用自动映射,但我认为您需要通过实现 IAutoMappingOverride 来映射 User 。像这样的东西:
I don't use the auto-mapping myself, but I think you need to map User by implementing IAutoMappingOverride. Something like: