NHibernate 删除我的行
NHibernate deleting my rows
我正在研究 NHibernate,当我测试它时,我尝试列出我的 Oracle 数据库中的所有数据。但是 NHibernate 正确访问了我的数据库,但删除了我的所有数据,而不是只读取我的数据。
(我可以连接到数据库,没有问题,只是它不问就删除了我所有的数据。)
这是我的代码:
NHibernateSession :
public class NHibernateSession
{
public static ISession OpenSession()
{
return CreateSessionFactory().OpenSession();
}
private static ISessionFactory CreateSessionFactory()
{
var cfg = OracleClientConfiguration.Oracle9
.ConnectionString(c =>
c.Is("DATA SOURCE=xe.world;PERSIST SECURITY INFO=True;USER ID=apismart;Password=APISMART"));
var sessionFactory = Fluently.Configure()
.Database(cfg)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<MesureMap>().ExportTo(@".\"))
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
return sessionFactory;
}
private static void BuildSchema(NHibernate.Cfg.Configuration config)
{
new SchemaExport(config)
.Create(false, true);
}
}
}
进入我的 MesuresController :
public int Get()
{
using (ISession session = NHibernateSession.OpenSession())
{
RucheRepository repo = new RucheRepository(session);
IQueryable<Ruche> result = repo.GetAll();
return result.Count();
}
}
存储库:
public class RucheRepository : IRucheRepository
{
protected ISession Session;
public RucheRepository(ISession session)
{
this.Session = session;
}
public Ruche Get(int idRuche)
{
return Session.Query<Ruche>().Where(ruche => ruche.Id == idRuche).FirstOrDefault<Ruche>();
}
public IQueryable<Ruche> GetAll()
{
return Session.Query<Ruche>();
}
}
问题来自您的 BuildSchema()
方法。每次执行此操作时,数据库将被删除并重新创建。改用这个:
private static void BuildSchema(NHibernate.Cfg.Configuration config)
{
new SchemaUpdate(config)
.Execute(false, true);
}
它只会更新您的模式,不会重新创建它。
正如@Rabban 接受的答案所说,new SchemaExport(config).Create(false, true);
是问题所在。
您正在做的称为代码优先方法,您首先编写所有数据库代码(特别是 models/entities)。 NHibernate 允许从这个 class 结构创建数据库。
Create
方法删除模式(如果已经存在)并创建新的。以下来自documentation:
Run the schema creation script; drop script is automatically executed before running the creation script.
查看同一文档中 Execute
的语法。它使您可以更好地控制此过程。
public void execute(boolean script,
boolean export,
boolean justDrop,
boolean justCreate)
以上文档适用于 Hibernate (Java);但它同样适用于 NHibernate (.net)。
我正在研究 NHibernate,当我测试它时,我尝试列出我的 Oracle 数据库中的所有数据。但是 NHibernate 正确访问了我的数据库,但删除了我的所有数据,而不是只读取我的数据。
(我可以连接到数据库,没有问题,只是它不问就删除了我所有的数据。)
这是我的代码:
NHibernateSession :
public class NHibernateSession
{
public static ISession OpenSession()
{
return CreateSessionFactory().OpenSession();
}
private static ISessionFactory CreateSessionFactory()
{
var cfg = OracleClientConfiguration.Oracle9
.ConnectionString(c =>
c.Is("DATA SOURCE=xe.world;PERSIST SECURITY INFO=True;USER ID=apismart;Password=APISMART"));
var sessionFactory = Fluently.Configure()
.Database(cfg)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<MesureMap>().ExportTo(@".\"))
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
return sessionFactory;
}
private static void BuildSchema(NHibernate.Cfg.Configuration config)
{
new SchemaExport(config)
.Create(false, true);
}
}
}
进入我的 MesuresController :
public int Get()
{
using (ISession session = NHibernateSession.OpenSession())
{
RucheRepository repo = new RucheRepository(session);
IQueryable<Ruche> result = repo.GetAll();
return result.Count();
}
}
存储库:
public class RucheRepository : IRucheRepository
{
protected ISession Session;
public RucheRepository(ISession session)
{
this.Session = session;
}
public Ruche Get(int idRuche)
{
return Session.Query<Ruche>().Where(ruche => ruche.Id == idRuche).FirstOrDefault<Ruche>();
}
public IQueryable<Ruche> GetAll()
{
return Session.Query<Ruche>();
}
}
问题来自您的 BuildSchema()
方法。每次执行此操作时,数据库将被删除并重新创建。改用这个:
private static void BuildSchema(NHibernate.Cfg.Configuration config)
{
new SchemaUpdate(config)
.Execute(false, true);
}
它只会更新您的模式,不会重新创建它。
正如@Rabban 接受的答案所说,new SchemaExport(config).Create(false, true);
是问题所在。
您正在做的称为代码优先方法,您首先编写所有数据库代码(特别是 models/entities)。 NHibernate 允许从这个 class 结构创建数据库。
Create
方法删除模式(如果已经存在)并创建新的。以下来自documentation:
Run the schema creation script; drop script is automatically executed before running the creation script.
查看同一文档中 Execute
的语法。它使您可以更好地控制此过程。
public void execute(boolean script,
boolean export,
boolean justDrop,
boolean justCreate)
以上文档适用于 Hibernate (Java);但它同样适用于 NHibernate (.net)。