流利的NHibernate?我这样做正确吗? [英] Fluent NHibernate? Am I doing this correctly?

查看:112
本文介绍了流利的NHibernate?我这样做正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次使用Fluent NHibernate和NHibernate.自2000年以来,我一直使用内部编写的自定义书面映射器.大约2年前改用LinqToSQL,大约6个月前改用Entities.

I am new to using Fluent NHibernate and NHibernate for the first time. I've used a custom written mapper since about 2000 that was written in house. Made a switch to LinqToSQL about 2 years ago, and about 6 months ago to Entities.

我想看看Fluent/NHibernate提供什么.但是,我似乎无法使其正常运行.以下是我的类,它们的引用和ClassMap的副本.有人可以告诉我这个简单的实现是否正确?

I'd like to see what Fluent/NHibernate have to offer. However, I can't seem to get it to run correctly. The following is a copy of my classes, their references, the ClassMaps. Can someone tell me if this simple implementation is correct?

这是我的映射和对象类:

This is my mappings and object classes:

using System;

using FluentNHibernate.Mapping;

namespace MyData.Data.Mappings
{
    public class Login
    {
        public virtual int LoginId { get; private set; }
        public virtual string Username { get; set; }
        public virtual User User { get; set; }
    }

    public class LoginClassMap : ClassMap<Login>
    {
        public LoginClassMap()
        {
                Table("Logins");

                Id(d => d.LoginId).GeneratedBy.Guid();
                Map(d => d.Username).Not.Nullable().Length(50);

                HasOne(d => d.User).ForeignKey("UserId").Cascade.All();
         }
    }

    public class User
    {
        public virtual Guid UserId{ get; private set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual Login Login { get; set; }
    }

    public class UserClassMap : ClassMap<User>
    {
        public UserClassMap()
        {
            Table("Users");

            Id(d => d.UserId).GeneratedBy.Guid();
            Map(d => d.FirstName).Not.Nullable().Length(100);
            Map(d => d.LastName).Not.Nullable().Length(100);

            References(r => r.Login, "UserId").Not.Nullable();
        }
    }
}

这是我的存储库:

using System;
using System.Linq;

using NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate.Linq;

using MyData.Data.Mappings;

namespace MyData.Data.Repository
{
    public class LoginRepository
    {
        private readonly ISessionFactory _sessionFactory;
        ISession _session;

        public LoginRepository()
        {
            _sessionFactory = this.CreateSessionFactory();

            _session = _sessionFactory.OpenSession();
        }

        private ISessionFactory CreateSessionFactory()
        {
            string connString = "MyDataConnectionString";


        FluentConfiguration config = Fluently.Configure()
        .Database(MsSqlConfiguration.MsSql2005.ConnectionString(
           x => x.FromConnectionStringWithKey(connString)))
        .ExposeConfiguration(
            c => c.SetProperty("current_session_context_class", "webContext"))
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Login>());

            return config.BuildSessionFactory();
        }

        public IQueryable<Login> GetAllLogins()
        {
            return _session.Linq<Login>();
        }
    }
}

每当到达config.BuildSessionFactory()时,都会引发以下错误:

Whenever it gets to config.BuildSessionFactory() it throws the following error:

在创建SessionFactory时使用了无效或不完整的配置.

An invalid or incomplete configuration was used while creating a SessionFactory.

内部异常是:

调用的目标已引发异常.

Exception has been thrown by the target of an invocation.

这是解决此问题的正确方法吗?任何想法或调整将不胜感激!

Is this the correct way to approach this? Any ideas or tweaks would be greatly appreciated!

推荐答案

Sean,您不能在LoginId上使用GeneratedBy.Guid(),因为它是一个int.尝试使用其他生成器或更改LoginId的类型.

Sean, you cannot use GeneratedBy.Guid() on LoginId because it's an int. Try to use another generator or change the type of LoginId.

我怀疑您不能在UserId上使用私有集",因为它是ID.

I suspect you cannot use "private set" on UserId because it's the Id.

此外,HasOne(d => d.User).ForeignKey("UserId")意味着,如果从映射中公开数据库模型,则FK约束的名称将为"UserId".在我看来,您的意图是指定用于保存表"Users"上的PK的名称列.

Also, HasOne(d => d.User).ForeignKey("UserId") means that if you expose your database model from your mappings, the name of the FK constraint will be "UserId". It seems to me that your intention was to specify the name column that holds the PK on table "Users".

这些链接具有更多信息:

These links have more infos:

http://wiki.fluentnhibernate.org/Fluent_mapping

http://jagregory. com/writings/i-think-you-meanamanytoonesir/

一对一的流利nhibernate?

这篇关于流利的NHibernate?我这样做正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆