Nhibernate(Fluent) SchemaExport 和 SQlite 问题!
我正在将 Nhibernate(Fluent) 与 Sqlite 一起使用,并且我所做的一切都没有问题:
为每个表创建 POCO EntityClasses,如下所示:
<块引用>
:公共类 tbl_Manufactor : BusinessObject
; { 公共虚拟字符串名称{ get;放; } }
实体类全部继承自 BusinessObject,其中包含 ID 属性和简单的 CRUD 功能 我还有其他 6 个实体类
使用 FluentHibernate 创建映射,如下所示:
<块引用>使用Database.Entitites; 使用 FluentNHibernate.Mapping; 命名空间Database.Mappings { 公共类 tbl_ManufactorMap : ClassMap
; { 公共 tbl_ManufactorMap() { Id(x => x.ID); 地图(x => x.Name); } } } 像这样创建 Fluent Config:
var Config = Fluently.Configure() .数据库(SQLiteConfiguration.Standard .ConnectionString(c=>c.FromConnectionStringWithKey("dbconnection")) .ShowSql()) .映射(m => m.FluentMappings.AddFromAssemblyOf
()) .ExposeConfiguration(cfg => _configuration = cfg) sessionFactory = Config.BuildSessionFactory();
执行 SchemaExport 以创建架构:
<块引用>新 SchemaExport(_configuration) .创建(假,真);
然后 Nhibernate 使用正确的表创建了一个完全工作的 SQlite 数据库!
但后来我想添加 2 个实体类(我已经以与以前相同的方式完成了),但是当我运行测试(创建了我的第一个数据库模式)时,它没有将新类(表)添加到数据库中!
最疯狂的是,测试总是删除任何创建的数据库,并在每次运行测试时使用 SchemaExport 重新创建它。
有人可以帮助我吗?
I am using Nhibernate(Fluent) with Sqlite and it worked with no problems what I have done:
Create the POCO EntityClasses for each table like this:
public class tbl_Manufactor : BusinessObject<tbl_Manufactor> { public virtual string Name { get; set; } }
The Entity Classes all inherit from BusinessObject which contains the ID Property and simple CRUD functions
I have also other 6 Entity Classes
Create the Mappings with FluentHibernate like this:
using Database.Entitites; using FluentNHibernate.Mapping; namespace Database.Mappings { public class tbl_ManufactorMap : ClassMap<tbl_Manufactor> { public tbl_ManufactorMap() { Id(x => x.ID); Map(x => x.Name); } } }
Create the Fluent Config like this:
var Config = Fluently.Configure() .Database(SQLiteConfiguration.Standard .ConnectionString(c=>c.FromConnectionStringWithKey("dbconnection")) .ShowSql()) .Mappings(m => m.FluentMappings.AddFromAssemblyOf<tbl_Manufactor>()) .ExposeConfiguration(cfg => _configuration = cfg) sessionFactory = Config.BuildSessionFactory();
Do a SchemaExport to Create the Schema with:
new SchemaExport(_configuration) .Create(false, true);
Then Nhibernate created a fully working SQlite Database with the right tables!
But then I wanted to add 2 entity classes(I have done it in the same way as before) but when I run my test(which created my first database schema) it not add the new classes(tables) to the database!
The craziest thing is, that the test is always deleting any created database and recreate it with SchemaExport every time I run the test.
Could someone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以选择直接使用“执行”进行数据库更新:
编辑:确保映射类是公共的。
You also have the option of using Execute directly for database updates:
Edit: Make sure the mapping classes are public.
内存中的 SQLite 仅在连接的生命周期内存在。因此,对于每个新连接,您都需要从头开始。
我知道两个解决方案。一种是仅使用单个会话(和连接)进行单元测试。另一个是我所做的:我实现了一个连接提供程序,它始终返回相同的连接。
Sqlite in memory only exists for the life time of the connection. So for each new connection you need to start from the beginning.
I know two solutions for this. One is to only use a single session (and connection) for the unit tests. The other is what I did: I implemented a connection provider which always returns the same connection.
您可以检查以下内容:
- 新的实体类是否在同一个程序集中?
- 还要检查模型中是否存在循环。
You could check the following :
- Are the new entity classes in the same assembly ?
- Check also for a loop in the model.