【问题标题】:NHibernate Fluent C# Mapping IsusesNHibernate Fluent C# 映射问题
【发布时间】:2013-12-21 15:15:30
【问题描述】:

我像这样创建我的表:

CREATE TABLE [dbo].[Users](
Id int NOT NULL IDENTITY (1,1) PRIMARY KEY,
EmailAddress varchar(255),
FullName varchar(255),
Password varchar(255),
);

我的模型是:

public class UserModel : Entity
{
    public virtual string EmailAddress { get; set; }
    public virtual string FullName { get; set; }
    public virtual string Password { get; set; }
}

我的实体类是:

public abstract class Entity
{
    public virtual int Id { get; set; }
}

我的映射类如下所示:

public class UserModelMap : ClassMap<UserModel>
{
    public UserModelMap()
    {
        Table("Users");
        Id(x => x.Id);
        Map(x => x.EmailAddress);
        Map(x => x.FullName);
        Map(x => x.Password);
    }
}

并且我包含了我所有必须使用以下配置映射的类:

        private static void InitializeSessionFactory()
    {
        _sessionFactory = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008
             .ConnectionString(c => c.FromConnectionStringWithKey("DefalutConnection"))
            )
            .Mappings(m =>
                      m.FluentMappings
                          .AddFromAssemblyOf<Entity>())
            .ExposeConfiguration(cfg => new SchemaExport(cfg)
                                            .Create(true, true))
            .BuildSessionFactory();
    }

但是当我使用以下代码查询我的数据库时:

Session.Query<TEntity>();

TEntity 是我的用户模型,我没有从数据库中得到任何行。

我似乎无法解决这里的问题。

【问题讨论】:

    标签: c# sql-server nhibernate fluent-nhibernate


    【解决方案1】:

    我想说,问题就在这里:

    .ExposeConfiguration(cfg 
       => new SchemaExport(cfg).Create(true, true)) // here
    

    因为,正如 NHibernate 所说:一切正常,没有问题,也不例外。只是 NO 数据。我会说,你一直在重新创建模式。

    检查一下:

    其中一个答案的摘录:

    您可以使用 SchemaUpdate,它会改为更新架构。这是一篇关于它的博客文章:http://geekswithblogs.net/dotnetnomad/archive/2010/02/22/138094.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-15
      • 2011-08-08
      • 1970-01-01
      • 1970-01-01
      • 2011-07-17
      • 2011-04-10
      • 2010-11-30
      相关资源
      最近更新 更多