简体   繁体   English

NHibernate Fluent C#Mapping Isuses

[英]NHibernate Fluent C# Mapping Isuses

I create my table like: 我创建我的表像:

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

My model is: 我的模型是:

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

My entity class is: 我的实体类是:

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

My mapping class is simple as below: 我的映射类很简单,如下所示:

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

And i include all my classes the have to be mapped using this following configuraiton: 并且我使用以下配置包括我必须映射的所有类:

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

But when i query my database using the following code: 但是当我使用以下代码查询我的数据库时:

Session.Query<TEntity>();

Where TEntityis my user model i get no rows back from the database. 在TEntity是我的用户模型的地方,我没有从数据库中返回任何行。

I cant seem to work out what the issue is here. 我似乎无法解决这里的问题。

I would say, that the problem is here: 我会说,问题出在这里:

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

Because, as NHibernate says: everything is OK, no issue, no exception. 因为,正如NHibernate所说:一切都好, 没有问题,也没有例外。 Just NO data. 只是没有数据。 And you are recreating schema all the time, I would say. 我会说,你正在重建模式。

Check this: 检查一下:

An extract from one of the answers: 其中一个答案的摘录:

You could use SchemaUpdate, which will update the schema instead. 您可以使用SchemaUpdate,它将更新架构。 Here's a blog post about it: http://geekswithblogs.net/dotnetnomad/archive/2010/02/22/138094.aspx 这是一篇关于它的博客文章: http//geekswithblogs.net/dotnetnomad/archive/2010/02/22/138094.aspx

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

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