流畅的NHibernate 1.2 + NHibernate 3.0,指定表名选项

时间:2011-02-23 13:59:09

标签: .net fluent-nhibernate

我是NHibernate的新手,现在正在做我的第一个项目。假设我有一个名为“User”的非常简单的表,它有

  

UserID,FirstName,LastName,UserName,Password。

我创建了一个实体类:

    public class UserAccount { 
            public virtual int? UserID { 
                    get; 
                    private set; 
            } 


            [Required(ErrorMessage = "Please enter username.")] 
            public virtual string Username { 
                    get; 
                    private set; 
            } 


            [Required(ErrorMessage = "Please enter password.")] 
            public virtual string Password { 
                    get; 
                    private set; 
            } 


            public virtual string FirstName { 
                    get; 
                    private set; 
            } 


            public virtual string LastName { 
                    get; 
                    private set; 
            } 
    } 

我还创建了映射类:

    public class UserAccountMap : ClassMap<UserAccount> { 
            public UserAccountMap() { 
                    Table("User"); 
                    Id(x => x.UserID, "UserID"); 
                    Map(x => x.FirstName, "FirstName").Length(50) 
                            .Not.Nullable(); 
                    Map(x => x.LastName, "LastName").Length(50) 
                            .Not.Nullable(); 
                    Map(x => x.Username, "UserName").Length(50) 
                            .Not.Nullable(); 
                    Map(x => x.Password, "Password").Length(50) 
                            .Not.Nullable(); 
            } 
    } 

这里我尝试将UserAccount对象映射到User表。

这是我的配置:

            private static ISessionFactory CreateSessionFactory() { 
                    return Fluently.Configure() 
                            .Database(MsSqlConfiguration.MsSql2008 
                                    .ConnectionString(c => c.FromConnectionStringWithKey("TestDB")) 
                                    .Cache(c => c.UseQueryCache().UseMinimalPuts()) 
                                    .DefaultSchema("dbo") 
                                    .ShowSql()) 
                            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserAccount>()) 
                            .BuildSessionFactory(); 


            } 

这是我从数据库中选择的方法:

                    using (var session = CreateSessionFactory()) { 
                            var query = from a in session.QueryOver<UserAccount>() 
                                                    where a.Username == "myusername" 
                                                    select a; 

                            var oneAccount = query.SingleOrDefault(); 

                            return oneAccount; 
                    } 

但是当我运行应用程序时,我收到了异常消息:

NHibernate.Exceptions.GenericADOException was unhandled Message=could not execute query 
[ SELECT this_.UserID as UserID0_0_, this_.FirstName as FirstName0_0_, 
this_.LastName as LastName0_0_, this_.UserName as UserName0_0_, 
this_.Password as Password0_0_ FROM [UserAccount] this_ WHERE 
this_.UserName = @p0 ] 
Positional parameters:  #0>myusername 
.... 
  Source=NHibernate 
.... 
Message=Invalid object name 'UserAccount'. 

看起来我没有成功地将类映射到表名。任何线索为什么会这样?

0 个答案:

没有答案