简体   繁体   English

使用NHibernate在Web服务中“无法执行查询错误”

[英]“Could not execute query error” in Web Services with NHibernate

As the title says, I am getting this error: 正如标题所说,我收到了这个错误:

could not execute query 无法执行查询
[ SELECT this_.Id as Id5_0_, this_.Username as Username5_0_, this_.PasswordHash as Password3_5_0_, this_.Salt as Salt5_0_, this_.Token as Token5_0_, this_.TokenStamp as TokenStamp5_0_, this_.Role as Role5_0_ FROM User this_ ]" [选择this_.Id为Id5_0_,this_.Username为Username5_0_,this_.PasswordHash为Password3_5_0_,this_.Salt为Salt5_0_,this_.Token为Token5_0_,this_.TokenStamp为TokenStamp5_0_,this_.Role为Role5_0_ FROM User this_]“
error when breakpointing. 断点时的错误。

I am trying to make a simple login with user in database where the user types in username and password hash. 我试图用数据库中的用户进行简单登录,其中用户键入用户名和密码哈希。 I have a user data which has Admin on every column to debug with. 我有一个用户数据,每个列上都有Admin来调试。

Here's my code: 这是我的代码:

SQL Server database: SQL Server数据库:

User table: User表:

CREATE TABLE [dbo].[User] 
(
    [Id]           UNIQUEIDENTIFIER NOT NULL,
    [Username]     NVARCHAR (50)    NULL,
    [PasswordHash] CHAR (64)        NOT NULL,
    [Salt]         CHAR (64)        NOT NULL,
    [Role]         UNIQUEIDENTIFIER NOT NULL,
    [Token]        NVARCHAR (50)    NOT NULL,
    [TokenStamp]   DATETIME         NULL,
    CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_User_Role] FOREIGN KEY ([Role]) REFERENCES [dbo].[Role] ([Id])
);

Role table: Role表:

CREATE TABLE [dbo].[Role]  
(
    [Id]   UNIQUEIDENTIFIER NOT NULL,
    [Name] NVARCHAR (50)    NULL,
    CONSTRAINT [PK_Role] PRIMARY KEY CLUSTERED ([Id] ASC)
);

WebService.cs : WebService.cs

using System.Linq;
using System.Net;
using System.Web.Services;
using NHibernate;
using Models;

[WebService(Namespace = "http://LambdAlarm.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class LambdAlarmWebService : WebService
{
    [WebMethod]
    public HttpStatusCode Login(string username, string passwordHash)
    {
        var factory = SessionFactory.Instance;
        var session = factory.OpenSession();
        var result = session.QueryOver<User>().List<User>();
        var user = new User();
        var login = result.FirstOrDefault(u => u.Username == username);

        if (user.Username == login.Username)
        {
            return HttpStatusCode.OK;
        }

        return HttpStatusCode.NotFound;
    }
}

User model: User模型:

using System;

namespace Models
{
    public class User : EntityBase
    {
        public virtual string Username { get; set; }
        public virtual string PasswordHash { get; set; }
        public virtual string Salt { get; set; }
        public virtual Role Role { get; set; }
        public virtual string Token { get; set; }
        public virtual DateTime TokenStamp { get; set; }
    }
}

Role model: Role模型:

namespace Models
{
    public class Role : EntityBase
    {
        public virtual string Name { get; set; }
    }
}

EntityBase : (class with one Guid property inherited by all models) EntityBase :(具有一个由所有模型继承的Guid属性的类)

using System;

namespace Models
{
    public class EntityBase
    {
        public virtual Guid Id { get; set; }
    }
}

UserMap : (NHibernate mapping) UserMap :( NHibernate映射)

using FluentNHibernate.Mapping;
using Models;

namespace NHibernate.Mapping
{
    public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            Table("User");
            Id(x => x.Id).GeneratedBy.GuidComb();
            LazyLoad();
            References(x => x.Role).Column("Role");
            Map(x => x.Username).Column("Username");
            Map(x => x.PasswordHash).Column("PasswordHash").Not.Nullable();
            Map(x => x.Salt).Column("Salt").Not.Nullable();
            Map(x => x.Token).Column("Token").Not.Nullable();
            Map(x => x.TokenStamp).Column("TokenStamp");
        }
    }
}

RoleMap : RoleMap

using FluentNHibernate.Mapping;
using Models;

namespace NHibernate.Mapping
{
    public class RoleMap : ClassMap<Role>
    {
        public RoleMap()
        {
            Table("Role");
            Id(x => x.Id).GeneratedBy.GuidComb();
            LazyLoad();
            Map(x => x.Name).Column("Name");
        }
    }
}

SessionFactory : SessionFactory

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using Models;
using NHibernate.Conventions;

namespace NHibernate
{
    public static class SessionFactory
    {
        private static ISessionFactory _sessionFactory;

        public static ISessionFactory Instance
        {
            get
            {
                if (_sessionFactory == null)
                {
                    _sessionFactory = CreateSessionFactory();
                }

                return _sessionFactory;
            }
        }

        private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                        .Database(MsSqlConfiguration.MsSql2012
                            .ConnectionString(c => c.FromConnectionStringWithKey("DatabaseConnectionString")))
                        .Mappings(m =>
                        {
                            m.FluentMappings.Conventions.AddFromAssemblyOf<CustomForeignKeyConvention>();
                            m.FluentMappings.AddFromAssemblyOf<EntityBase>();
                        })
                        .BuildSessionFactory();
        }
    }
}

Conventions : Conventions

using FluentNHibernate;
using FluentNHibernate.Conventions;

namespace NHibernate.Conventions
{
    public class CustomForeignKeyConvention : ForeignKeyConvention
    {
        protected override string GetKeyName(Member property, System.Type type)
        {
            if (property == null)
            {
                return type.Name;
            }

            return property.Name;
        }
    }
}

So that's pretty much it. 所以这就是它。 If there is anyone who can help out, would be much appreciated... 如果有人可以提供帮助,我将不胜感激......

User is a reserved word in sql. 用户是sql中的保留字。 You can rename your table or make nhibernate use `` around the table name. 你可以重命名你的表或使用nhibernate使用``围绕表名。 I guess that if you change 我想如果你改变了

Table("User"); 

to

Table("`User`");

it will work. 它会工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM