.net - Fluent NHibernate 1.2 + NHibernate 3.0, specify table name Options -
i new nhibernate, , doing first project. have simple table named "user", has columns of
userid, firstname, lastname, username, password.
and created entity class:
public class useraccount { public virtual int? userid { get; private set; } [required(errormessage = "please enter username.")] public virtual string username { get; private set; } [required(errormessage = "please enter password.")] public virtual string password { get; private set; } public virtual string firstname { get; private set; } public virtual string lastname { get; private set; } }
i created mapping class:
public class useraccountmap : classmap<useraccount> { public useraccountmap() { table("user"); id(x => x.userid, "userid"); map(x => x.firstname, "firstname").length(50) .not.nullable(); map(x => x.lastname, "lastname").length(50) .not.nullable(); map(x => x.username, "username").length(50) .not.nullable(); map(x => x.password, "password").length(50) .not.nullable(); } }
here try map useraccount object user table.
this configuration:
private static isessionfactory createsessionfactory() { return fluently.configure() .database(mssqlconfiguration.mssql2008 .connectionstring(c => c.fromconnectionstringwithkey("testdb")) .cache(c => c.usequerycache().useminimalputs()) .defaultschema("dbo") .showsql()) .mappings(m => m.fluentmappings.addfromassemblyof<useraccount>()) .buildsessionfactory(); }
and method select database:
using (var session = createsessionfactory()) { var query = in session.queryover<useraccount>() a.username == "myusername" select a; var oneaccount = query.singleordefault(); return oneaccount; }
but when run application, got exception message:
nhibernate.exceptions.genericadoexception unhandled message=could not execute query [ select this_.userid userid0_0_, this_.firstname firstname0_0_, this_.lastname lastname0_0_, this_.username username0_0_, this_.password password0_0_ [useraccount] this_ this_.username = @p0 ] positional parameters: #0>myusername .... source=nhibernate .... message=invalid object name 'useraccount'.
it looks have not mapped class table name. clue why that?
Comments
Post a Comment