加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Fluent NHibernate自动删除数据

发布时间:2020-12-16 01:53:20 所属栏目:百科 来源:网络整理
导读:我在ASP.NET MVC4应用程序中使用PostgreSQL和Fluent NHibernate以及代码优先实体和映射. 当我运行应用程序时,它会自动删除数据库中的每条记录. 这是我的NHibernateHelper类 public class NHibernateHelper{ private static ISessionFactory _sessionFactory;
我在ASP.NET MVC4应用程序中使用PostgreSQL和Fluent NHibernate以及代码优先实体和映射.

当我运行应用程序时,它会自动删除数据库中的每条记录.

这是我的NHibernateHelper类

public class NHibernateHelper
{
    private static ISessionFactory _sessionFactory;

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

    private static void InitializeSessionFactory()
    {
        _sessionFactory = Fluently.Configure()
            .Database(PostgreSQLConfiguration.Standard
                        .ConnectionString(
                            @"Server=localhost;Port=5432;Database=TestDB;User=postgres;Password=postgre;")
                        .ShowSql()
            )
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ProductCategory>())
            .ExposeConfiguration(cfg => new SchemaExport(cfg)
                            .Create(true,true))
            .BuildSessionFactory();
    }

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

有没有错误的配置?

解决方法

我会说问题出在这里:

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

每次构建会话工厂时,您似乎都在导出模式.现在我不是100%肯定(我从来没有使用代码优先生成表,只映射到我之前创建的现有数据库),但我打赌导出会覆盖你已经拥有的东西(丢弃和重新创建或者其他) ).

Here在使用Fluent NHibernate导出模式时,有人遇到类似SQLite文件被覆盖的问题,所以我要说你必须小心你何时以及如何(重新)创建数据库:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读