c# - Search in Fluent-NHibernate using session.QueryOver<> return empty -
hey new orm, im using fluent nhibernate in crud seems have problem in search, return empty or null value , can insert data, when insert record seems updating bcoz replace previous record. try doing google still no used. can give me tutorial links.
my code:
employee class in objclass folder
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.. } } my mapping class in map class folder
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.. } } } my repository in repository folder
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(); } } } } my 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 { { 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(); } } } my code snipe
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 record"); } private void bntse_click(object sender, eventargs e) { employees emp = repo.getemployeesbylname(1); messagebox.show(emp.last_name); } } } finally table
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 :( sorry poor english , alignment :( in advance
on first sight, noticed 2 possible problems code.
first:
have tried profile generated sql? profler tool nhprof.
edit:
another option logging be, if extend fluentconfiguration setup adding:
.diagnostics(d => d.enable(true)) .diagnostics(d => d.outputtoconsole()) where first line enables conditional logging, second line sets diagnostic logging console.
so see going on in background, doubt replacing existing record.
since calling save() method
persists given transient instance
however stating gets "replaced", equivalent call saveorupdate() method which
either save() or update() given instance, depending upon value of identifier property.
second:
do need create whole database schema each time instantiate nhibernate helper?
because calling exposeconfiguration() method schemaexport.create(true, true).
where schemaexport.create(...)
runs schema creation script
by passing second boolean parameter create(), telling execute creation of schema.
in short means drop , re-create tables on every run.
so if connecting existing schema, comment out schemaexport call, unless using example in-memory sql server.
hope helps!
p.s.: quotes taken fluent nhibernates xmldoc signatures.
Comments
Post a Comment