正如标题所说,我收到了这个错误:
无法执行查询
[选择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_]&#34 ;
断点时的错误。
我正在尝试使用数据库中的用户进行简单登录,其中用户键入用户名和密码哈希。我有一个用户数据,每个列上都有Admin来调试。
这是我的代码:
SQL Server数据库:
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
表:
CREATE TABLE [dbo].[Role]
(
[Id] UNIQUEIDENTIFIER NOT NULL,
[Name] NVARCHAR (50) NULL,
CONSTRAINT [PK_Role] PRIMARY KEY CLUSTERED ([Id] ASC)
);
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
型号:
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
型号:
namespace Models
{
public class Role : EntityBase
{
public virtual string Name { get; set; }
}
}
EntityBase
:(所有模型都继承了一个Guid
属性的类)
using System;
namespace Models
{
public class EntityBase
{
public virtual Guid Id { get; set; }
}
}
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
:
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
:
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
:
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;
}
}
}
这就是它。如果有人可以提供帮助,我将不胜感激......
答案 0 :(得分:3)
用户是sql中的保留字。你可以重命名你的表或使用nhibernate使用``围绕表名。我想如果你改变了
Table("User");
到
Table("`User`");
它会起作用。