c# - Entity Framework 5 code first cannot get model to work -
tearing hair out again on one...using ef 5 code first, building model in code - compiles, syntactically correct, exception when code builds model. here entity class (i have vaidation attributes, have removed them here readability):
[table("users")] public class user : ipentity { #region constructor (needs initialize list objects related entities) public user() { this.profiles = new list<profile>(); this.profiledivs = new list<profilediv>(); this.profiledepts = new list<profiledept>(); } #endregion #region entity properties , validation attributes [key] public long userid { get; set; } public long pclientid { get; set; } public string username { get; set; } public string userdescription { get; set; } public long? empid { get; set; } public string mustchangepassword { get; set; } public long? failedlogins { get; set; } public datetime? lastlogin { get; set; } public long? sequencenumber { get; set; } public string alldivs { get; set; } public string alldepts { get; set; } public string userrole { get; set; } public datetime? beginsupport { get; set; } public datetime? endsupport { get; set; } public string onetimeaccess { get; set; } public long? clonedfromuser { get; set; } public string email { get; set; } public string resetemail { get; set; } public datetime? resettimeout { get; set; } public long? challengefailures { get; set; } public string permuserrole { get; set; } public datetime? passwordchangeddate { get; set; } public virtual icollection<profile> profiles { get; set; } public virtual icollection<profilediv> profiledivs { get; set; } public virtual icollection<profiledept> profiledepts { get; set; } public virtual worksession worksession { get; set; } } the model builder class is:
public class user_map : entitytypeconfiguration<user> { public user_map() { this.totable("users"); this.haskey(t => new { t.userid }); this.property(t => t.userid) .hascolumnname("user_id") .hasdatabasegeneratedoption(databasegeneratedoption.none) .isrequired(); this.property(t => t.username) .hascolumnname("username") .isrequired() .hasmaxlength(25); this.property(t => t.pclientid) .hascolumnname("pclient_id"); this.property(t => t.empid) .hascolumnname("emp_id"); this.property(t => t.mustchangepassword) .hascolumnname("must_change_password") .hasmaxlength(1); this.property(t => t.userdescription) .hascolumnname("user_description") .hasmaxlength(80); this.property(t => t.failedlogins) .hascolumnname("failed_logins"); this.property(t => t.lastlogin) .hascolumnname("last_login"); this.property(t => t.sequencenumber) .hascolumnname("sequence_number"); this.property(t => t.alldivs) .hascolumnname("all_divs") .hasmaxlength(1); this.property(t => t.alldepts) .hascolumnname("all_depts") .hasmaxlength(1); this.property(t => t.userrole) .hascolumnname("user_role") .hasmaxlength(2); this.property(t => t.beginsupport) .hascolumnname("begin_support"); this.property(t => t.endsupport) .hascolumnname("end_support"); this.property(t => t.onetimeaccess) .hascolumnname("one_time_access") .hasmaxlength(1); this.property(t => t.clonedfromuser) .hascolumnname("cloned_from_user"); this.property(t => t.email) .hascolumnname("email") .hasmaxlength(60); this.property(t => t.resetemail) .hascolumnname("reset_email") .hasmaxlength(60); this.property(t => t.resettimeout) .hascolumnname("reset_timeout"); this.property(t => t.challengefailures) .hascolumnname("challenge_failures"); this.property(t => t.permuserrole) .hascolumnname("perm_user_role") .hasmaxlength(2); this.property(t => t.passwordchangeddate) .hascolumnname("password_changed_date"); this.hasoptional(t => t.worksession) .withrequired(t => t.user); // todo: syntactically correct model blows up! this.hasmany(t => t.profiles) .withrequired(t => t.user) .hasforeignkey(t => t.userid); } } when model builder class' constructor executes, following exception thrown on line above (after comment):
the expression 't => t.user' not valid property expression. expression should represent property: c#: 't => t.myproperty' vb.net: 'function(t) t.myproperty'. the profiles entity simple:
[table("profile")] public class profile : ipentity { [key, column(order = 0)] [foreignkey("userid")] public long userid { get; set; } [key, column(order = 1)] public string functionalarea { get; set; } public int rightsid { get; set; } public user user; } i have been beating on 2 or 3 days, if can spot mistake, appreciative!
thanks, peter
update: found 1 bone-headed mistake reading this post, since had declared field , not property...but different exception not understand:
the navigation property 'userid' not declared property on type 'profile'. verify has not been explicitly excluded model , valid navigation property. this has me confused userid declared property on profile...?
second update:
i understand exception (since there no inner exception detail) cannot determine coming from. changed user_map class include:
this.hasmany(t => t.profiles) .withrequired(t => t.user) .hasforeignkey(t => t.userid) .willcascadeondelete(false); and profile_map class include:
this.hasrequired(t => t.user) .withmany() .hasforeignkey(t => t.userid) .willcascadeondelete(false); since have work existing database, stuck property "userid" being foreign key in both tables (in user table foreign key profiles table, in profiles table foreign key user table.) added .willcascadeondelete(false) both in case there kind of circularity problem. said, cannot determine map blowing things up, calls both constructors pass without exception, when override onmodelcreating in context exits exception thrown. here override:
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { var mbprofile = new profile_map(); modelbuilder.configurations.add(mbprofile); var mbuser = new user_map(); modelbuilder.configurations.add(mbuser); base.onmodelcreating(modelbuilder); } i know i'm still ef newbie, cannot find problem (well, haven't yet anyway...)
thanks again.
public user user; should property instead of field - others :)
public user user { get; set; } with virtual preferably (to allow lazy loading), because have marked navigation properties virtual.
edit update
the exception talks navigation property userid , complains not "valid navigation property". indeed isn't valid navigation property. exception indicates possibly have used userid (instead of user) in withrequired method. should take closer @ that.
Comments
Post a Comment