“在Web服务中创建SessionFactory时使用了无效或不完整的配置”NHibernate

时间:2014-12-03 14:01:46

标签: c# .net sql-server web-services nhibernate

正如标题所说,我收到一条错误消息:“在创建SessionFactory时使用了无效或不完整的配置。检查PotentialReasons集合,以及InnerException以获取更多详细信息。”

我将从代码开始。

数据库:

用户表:

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

角色表:

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

用户类:

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 Guid Role { get; set; }
            public virtual string Token { get; set; }
            public virtual DateTime TokenStamp { get; set; }
        }
    }

角色类:

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

EntityBase:

using System;

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

用户地图:

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

角色地图:

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

CustomForeignKeyConvention:

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

会议工厂:

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
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<Models.EntityBase>();
                        })
                        .BuildSessionFactory();
        }
    }
}

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 password)
    {
        var factory = SessionFactory.Instance;
        var session = factory.OpenSession();
        var result = session.QueryOver<User>().List<User>();

        var login = result.Where(u => u.Username == username).Where(p => p.PasswordHash == password);

        return HttpStatusCode.NotFound;
    }
}

所以是的......这不是一件容易的事。我认为数据库中的外键关系与User和Role表有关。角色列应引用角色表中的Id。

请帮忙!

3 个答案:

答案 0 :(得分:7)

问题是References()是指引用另一个实体,而不是类型。如果您想使用某种类型,则应使用Map(x => x.Role)代替References(x => x.Role)

User更改为:

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; } //CHANGED HERE
    public virtual string Token { get; set; }
    public virtual DateTime TokenStamp { get; set; }
}

您的UserMap

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

此外,在使用Fluent NHibernate进行映射时,没有理由指定列名。默认情况下,它只会使用您的媒体资源的名称,因此Map(x => x.Name).Column("Name").Not.Nullable()之类的内容与Map(x => x.Name).Not.Nullable()

相同

其他一些建议:

为您的映射创建基类以使其更容易:

public abstract class ClassMapBase<T> : ClassMap<T> where T: EntityBase
{
    protected ClassMapBase()
    {
        Id(x => x.Id).Not.Nullable().GeneratedBy.GuidComb();
        LazyLoad();
    }
}

另外,我很惊讶NHibernate没有抱怨你Id EntityBase有一个公共制定者。它应该设置为受保护或私有。除非您希望它与实体名称不同,否则也无需指定您的表名称

答案 1 :(得分:3)

虽然有正确答案,但我会让其他人认为有用的其他解决方案。当您的应用程序无法访问您的数据库时,会导致此错误。所以:

  1. 签入services.exe并查看您的SQL Server是否正在运行
  2. 或简单解决方案,检查您的连接字符串是否指向您的数据库。

答案 2 :(得分:0)

如果其他人遇到这种错误,请检查您的实体中是否存在虚拟关键字。例如:

<asp:Button ID="btSubmit" runat="server" Text ="Submit Email" OnClick="btSubmit_Click" OnClientClick="return confirm('Test');"/>