流畅的NHibernate映射抛出了一个例外,“Id”未映射'

时间:2015-02-13 10:37:47

标签: nhibernate fluent-nhibernate fluent-nhibernate-mapping

我需要Fluent Nhibernate中的自动映射帮助。这是我想在我的应用程序中使用的表格(它们中有很多,但我想从仅映射其中的几个开始)

好吧,我想使用AutoMapping功能,因为我不想为超过100个表编写映射类......

无论如何,这是创建SessionFactory时抛出的错误(代码在本文末尾)

  

实体'FilterConfig'没有映射的Id   使用Id方法映射您的标识属性。例如:Id(x => x.Id)。

enter image description here

实体(我希望我正确创建):

public partial class UserLogin
{
    public UserLogin()
    {
        this.UserMessages = new List<UserMessage>();
        this.UserMessagesReceivers = new List<UserMessagesReceiver>();
    }

    public virtual int ID { get; set; }
    public virtual int UserTypeID { get; set; }
    public virtual int? StudentID { get; set; }
    public virtual int? HeadmasterID { get; set; }
    public virtual int? ParentID { get; set; }
    public virtual string UniqueID { get; set; }
    public virtual bool ShowMyPhoneNumber { get; set; }
    public virtual bool IsBanned { get; set; }
    public virtual string Login { get; set; }
    public virtual string Password { get; set; }
    public virtual bool WasPasswordSent { get; set; }
    public virtual string Email { get; set; }
    public virtual string UserPicture { get; set; }
    public virtual IList<UserMessage> UserMessages { get; set; }
    public virtual IList<UserMessagesReceiver> UserMessagesReceivers { get; set; }
}

public partial class UserMessage
{
    public UserMessage()
    {
        this.UserMessagesReceivers = new List<UserMessagesReceiver>();
        this.UserMessagesReplies = new List<UserMessagesReply>();
    }

    public virtual int ID { get; set; }
    public virtual DateTime Date { get; set; }
    public virtual DateTime? LastCheckDate { get; set; }
    public virtual int UserLoginID { get; set; }
    public virtual string Description { get; set; }
    public virtual bool HasNonCheckedReplies { get; set; }
    public virtual UserLogin UserLogin { get; set; }
    public virtual IList<UserMessagesReceiver> UserMessagesReceivers { get; set; }
    public virtual IList<UserMessagesReply> UserMessagesReplies { get; set; }
}

public partial class UserMessagesReceiver
{
    public UserMessagesReceiver()
    {
        this.WasMessageChecked = false;
        this.UserMessagesReplies = new List<UserMessagesReply>();
    }

    public virtual int ID { get; set; }
    public virtual int UserMessagesID { get; set; }
    public virtual int ReceiverLoginID { get; set; }
    public virtual bool WasMessageChecked { get; set; }
    public virtual DateTime? LastCheckedDate { get; set; }
    public virtual UserLogin UserLogin { get; set; }
    public virtual UserMessage UserMessage { get; set; }
    public virtual IList<UserMessagesReply> UserMessagesReplies { get; set; }
}

public partial class UserMessagesReply
{
    public UserMessagesReply()
    {
    }

    public virtual int ID { get; set; }
    public virtual DateTime Date { get; set; }
    public virtual int UserMessagesID { get; set; }
    public virtual int? UserMessagesReceiverID { get; set; }
    public virtual string Description { get; set; }
    public virtual UserMessage UserMessage { get; set; }
    public virtual UserMessagesReceiver UserMessagesReceiver { get; set; }
}

配置:

    public class AutomappingConfiguration : DefaultAutomappingConfiguration
    {
        public override bool IsId(Member member)
        {
            return member.Name == member.DeclaringType.Name + "ID";
        }
    }

    private static AutoPersistenceModel CreateAutomappings()
    {
        return AutoMap.AssemblyOf<AutomappingConfiguration>(new AutomappingConfiguration());
    }

    private static ISessionFactory CreateSessionFactory()
    {
        var cfg = new AutomappingConfiguration();
        return Fluently.Configure()
                       .Database(MySQLConfiguration.Standard
                       .ConnectionString("..."))
                       .Mappings(m => m.AutoMappings
                                       .Add(AutoMap.AssemblyOf<UserLogin>(cfg))
                                       .Add(AutoMap.AssemblyOf<UserMessage>(cfg))
                                       .Add(AutoMap.AssemblyOf<UserMessagesReceiver>(cfg))
                                       .Add(AutoMap.AssemblyOf<UserMessagesReply>(cfg)))
                       .BuildSessionFactory();
    }

1 个答案:

答案 0 :(得分:1)

在配置中你说Ids被命名为UserLoginID,但在课堂上它们被定义为public virtual int ID { get; set; }所以改变

return member.Name == member.DeclaringType.Name + "ID";

return member.Name == "ID";

其他一些信息:

  • 似乎没有使用CreateAutomappings()
  • AutomappingConfiguration应该至少覆盖ShouldMap(成员)来过滤命名空间(例如member.Namespace.StartsWith(typeof(UserMessage).Namespace)),否则迟早会映射实用程序类等
  • AutoMap.AssemblyOf&LT;&GT;应该按照包含不是每种类型的类型的程序集调用