我正在研究NHibernate,當我測試它時,我嘗試將所有數據列在我的Oracle數據庫中。但NHibernate正確訪問我的數據庫,但刪除我的所有數據,而不是隻讀取我的數據。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>();
}
}
SchemaUpdate沒有「.Create」方法:/ – Orionss
@Orionss對不起,我的意思是'執行',我更新了答案。 – Rabban
我工作!謝謝! – Orionss