![](/img/trans.png)
[英]Fluent NHibernate - column name from property in Session.QueryOver
[英]Search in Fluent-NHibernate using session.QueryOver<> return empty
嘿,我剛接觸ORM的每個人,我在我的CRUD中使用Fluent NHibernate時似乎在搜索時遇到問題,它將返回一個空或null值,並且我也可以插入數據,但是當我插入另一個記錄時,它似乎在更新bcoz它替換我以前的記錄。 我嘗試做一些谷歌,但仍然沒有用。 誰能給我一些教程鏈接。
我的代碼:
objclass文件夾中的employee Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace fluent.objclass
{
public class employees
{
public virtual int employee_id { get; set; }
public virtual string employee_code { get; set; }
public virtual string last_name { get; set; }
public virtual string first_name { get; set; }
public virtual string middle_initial { get; set; }
ect..
}
}
我的地圖課程在地圖課程文件夾中
using fluent.objclass;
using FluentNHibernate.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace fluent.mapclass
{
public class employeesMap: ClassMap<employees>
{
public employeesMap()
{
Id(x => x.employee_id);
Map(x => x.employee_code);
Map(x => x.last_name);
Map(x => x.first_name);
Map(x => x.middle_initial);
ect..
}
}
}
我在存儲庫文件夾中的存儲庫
using fluent.objclass;
using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace fluent.repository
{
public class emp_repository
{
public void INSERT(employees newEmp)
{
using (ISession session = NHibernateHelper.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(newEmp);
transaction.Commit();
}
}
}
public employees GetemployeesbyLName(int input)
{
using (ISession session = NHibernateHelper.OpenSession())
{
var result = session.QueryOver<employees>().Where(x => x.employee_id == input).SingleOrDefault();
return result ?? new employees();
}
}
}
}
我的NHibernateHelper
using fluent.objclass;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace fluent
{
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(MsSqlConfiguration.MsSql2012
.ConnectionString(@"Server=ARK\DARKAGE;Database=PNH;Trusted_Connection=True;")
.ShowSql()
)
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<employees>())
.ExposeConfiguration(cfg => new SchemaExport(cfg)
.Create(true, true))
.BuildSessionFactory();
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
}
我的代碼被狙擊
using FluentNHibernate.Mapping;
using fluent.objclass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NHibernate.Linq;
using fluent.repository;
namespace fluent
{
public partial class fluent : Form
{
emp_repository repo = new emp_repository();
public fluent()
{
InitializeComponent();
}
private void bntADD_Click(object sender, EventArgs e)
{
var emp = new employees
{
employee_code = txtBAR.Text.Trim(' '),
last_name = txtLNM.Text.Trim(' '),
first_name = txtFNM.Text.Trim(' '),
middle_initial = txtMNM.Text.Trim(' '),
ect...
};
repo.INSERT(emp);
MessageBox.Show(txtLNM.Text.Trim(' ') + "Successfully Added To Record");
}
private void bntSE_Click(object sender, EventArgs e)
{
employees emp = repo.GetemployeesbyLName(1);
MessageBox.Show(emp.last_name);
}
}
}
最后我的桌子
USE [PNH]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[employees](
[employee_id] [int] IDENTITY(1,1) NOT NULL,
[employee_code] [nvarchar](255) NULL,
[last_name] [nvarchar](255) NULL,
[first_name] [nvarchar](255) NULL,
[middle_initial] [nvarchar](255) NULL,
ect...
PRIMARY KEY CLUSTERED
(
[employee_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
:(對不起,我的英語不佳和對齊方式:(預先感謝
我的錯誤在NHibernateHelper.cs中
ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true);
每當我運行並執行它時,它都會刪除當前表並重新創建,這就是為什么我每次添加新條目時搜索都為空並且添加替換的原因
我正確的NHibernateHelper.cs
using fluent.objclass;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace fluent
{
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static readonly object factorylock = new object();
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
InitializeSessionFactory();
return _sessionFactory;
}
}
private static void InitializeSessionFactory()
{
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012
.ConnectionString(@"Server=ARK\DARKAGE;Database=PNH;Trusted_Connection=True;").ShowSql()
)
.Mappings(m =>
m.FluentMappings
.AddFromAssembly(Assembly.GetExecutingAssembly()))
.BuildSessionFactory();
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
}
乍一看,我實際上注意到您的代碼有兩個可能的問題。
第一:
您是否嘗試分析生成的SQL? 使用像NHProf這樣的分析工具。
編輯:
如果您通過添加以下內容來擴展FluentConfiguration
設置,則可以進行日志記錄的另一種選擇:
.Diagnostics(d => d.Enable(true))
.Diagnostics(d => d.OutputToConsole())
第一行啟用條件日志記錄,第二行將診斷日志記錄設置為控制台。
這樣一來,您就可以真正看到后台發生的事情,因為我懷疑它實際上會取代您現有的記錄。
由於您正在調用Save()
方法,
堅持給定的瞬時實例
但是,您要聲明它已被“替換”,這等效於調用SaveOrUpdate()
方法,
給定實例的Save()或Update()取決於其標識符屬性的值。
第二:
每次實例化NHibernate
幫助程序時,是否需要創建整個數據庫架構?
因為您正在使用SchemaExport.Create(true, true)
調用ExposeConfiguration()
方法。
哪里SchemaExport.Create(...)
運行架構創建腳本
通過將第二個布爾參數傳遞給Create()
,您實際上是在告訴執行架構的創建。
簡而言之,這意味着它將在每次運行時刪除並重新創建表。
因此,如果要連接到已存在的架構,則可以注釋掉SchemaExport
調用,除非您正在使用此示例(例如內存中的sql服務器)。
希望能幫助到你!
ps:引自Fluent Nhibernates XMLdoc簽名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.