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

发布于 2024-10-18 20:27:16 字数 3427 浏览 0 评论 0原文

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

用户 ID、名字、姓氏、用户名、密码。

我创建了一个实体类:

    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'. 

看起来我没有成功地将类映射到表名。有什么线索吗?

I am new to NHibernate, and am currently doing my first project. Say I have a very simple table named "User", it has columns of

UserID, FirstName, LastName, UserName, Password.

And I created an entity class:

    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; 
            } 
    } 

I also created mapping class:

    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(); 
            } 
    } 

Here I try to map UserAccount object to User table.

This is my configuration:

            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(); 


            } 

And this is my method to select from database:

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

                            var oneAccount = query.SingleOrDefault(); 

                            return oneAccount; 
                    } 

But when I run the application, I got exception message:

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'. 

It looks like I have not successfully mapped class to table name. Any clue why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文