sql-server-2008 - Fluent NHibernate - 总是删除表

标签 sql-server-2008 nhibernate configuration fluent-nhibernate

嗨,我开始学习 Fluent NHibernate。我正在使用这个教程http://www.d80.co.uk/post/2011/02/20/Linq-to-NHibernate-Tutorial.aspx .

这是我的示例代码:

public class Account
{
    public virtual int Id { get; set; }
    public virtual string Nick { get; set; }
    public virtual string Password { get; set; }
}

public class AccountMap:ClassMap<Account>
{
    public AccountMap()
    {
        Id(x => x.Id);
        Map(x => x.Nick);
        Map(x => x.Password);
    }
}

public class  NHiberanteHelper
{
    private static ISessionFactory _sessionFactory;

    private static ISessionFactory SessionFactory
    {
        get
        {
            if (_sessionFactory == null)
                InitializeSessionFactory();

            return _sessionFactory;
        }
    }

    private  static void InitializeSessionFactory()
    {

        _sessionFactory = Fluently.Configure()

            //NHibernate bude pouzivat ovladace pre MS SQL 2008
            .Database(MsSqlConfiguration.MsSql2008
                          .ConnectionString(
                               @"Server=JAN-MSI\SQLEXPRESS;Database=SimpleNHibernate;Trusted_Connection=True;").ShowSql()
                              )

            //urci NHibernatu kde ma hladat mapovacie subory
            .Mappings(m=>m.FluentMappings.AddFromAssemblyOf<Account>())

            //ak tabs nie su v DB vytvori
            .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))

            //vytvori jeden session pre cely life-time apps
            .BuildSessionFactory();
    }

    public static ISession OpenSession()
    {
        return SessionFactory.OpenSession();
    }
}

class Program
{
    static void Main(string[] args)
    {
        using (var session = NHiberanteHelper.OpenSession())
        {
            using (var trans = session.BeginTransaction())
            {
                var account = new Account
                                  {
                                      Nick = "dfdwf",
                                      Password = "xxx"
                                  };
                session.Save(account);
                trans.Commit();

            }
        }
        Console.ReadKey();
    }
}

问题是这个 Fluent 配置总是删除数据库中的表。

我只需要检查表是否不存在,因此在代码运行删除表时并不总是创建表。

最佳答案

您对 new SchemaExport(cfg).Create(true, true) 的调用正在将配置导出到数据库。这将删除并重新创建它(它不够聪明,无法找出差异并执行它们。

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

我总是更喜欢自己更新表或使用 Redgate 的 SQLCompare 等工具来更新架构,同时保留数据。

关于sql-server-2008 - Fluent NHibernate - 总是删除表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7566728/

相关文章:

sql - 递归 SQL 查询加速非索引查询

NHibernate - 哪里存在(X)

asp.net-mvc - 在 IIS 7/Server 2008 上部署 ASP.NET MVC 应用程序的最佳方式?

windows - 在 Windows 上安装应用程序期间捕获所有更改

SQL Server 2000 与 SQL Server 2008 查询性能

sql-server - 如何在存储过程中使用函数

sql - 在 CASE 语句中使用 LIKE

asp.net-mvc - 温莎 + NHibernate + ISession + MVC

python - 元类配置类。但是我们可以配置元类吗?

linux - 用户 Centos 8 Postgresql12 的身份验证失败