c# - DbUpdateException when seeding data -
i'm trying populate code first database admin user business relationship myself can access system.
to this, i'm calling seed
method in global.asax file following:
public void seed() { if (!roles.any()) { list<role> roles = new list<role> { new role { rolename = "admin" }, new role { rolename = "user" } }; foreach (role r in roles) { roles.add(r); } savechanges(); } if (!users.any()) { user u = new user(); u.emailaddress = "my@email.address"; u.username = "ortund"; u.password = hashing.createhash("p455w0rd"); u.role = roles.single(r => r.rolename == "admin"); users.add(u); savechanges(); } }
user , role defined follows:
namespace logan.web.objects { public class user : loganbaseobject<user> { public string username { get; set; } public string emailaddress { get; set; } public string password { get; set; } public string biography { get; set; } public virtual role role { get; set; } public virtual icollection<article> articles { get; set; } public user() { username = string.empty; emailaddress = string.empty; password = string.empty; biography = string.empty; articles = new list<article>(); } } } public class role : loganbaseobject<role> { public string rolename { get; set; } public virtual icollection<user> users { get; set; } public role() { rolename = string.empty; users = new list<user>(); } } } namespace logan.web.dbcontext { public class roledbcontext : logandbbaseobject<role> { private static webdbcontext db = new webdbcontext(); public roledbcontext() : base() { property(p => p.rolename) .hascolumnname("srolename") .isrequired(); hasmany(m => m.users) .withrequired(); totable("roles"); } } public class userdbcontext : logandbbaseobject<user> { private static webdbcontext db = new webdbcontext(); public userdbcontext() : base() { property(p => p.username) .hascolumnname("susername") .hasmaxlength(20) .isrequired(); property(p => p.emailaddress) .hascolumnname("semailaddress") .hasmaxlength(200) .isrequired(); property(p => p.password) .hascolumnname("spassword") .hasmaxlength(255) .isrequired(); property(p => p.biography) .hascolumnname("sbiography") .hascolumntype("text"); hasrequired(r => r.role) .withmany(m => m.users) .map(x => x.mapkey("fkroleid")) .willcascadeondelete(false); property(p => p.createdate) .hascolumntype("datetime"); totable("users"); } } }
when seed
method above gets savechanges(), next error:
an error occurred while saving entities not expose foreign key properties relationships. entityentries property homecoming null because single entity cannot identified source of exception. handling of exceptions while saving can made easier exposing foreign key properties in entity types. see innerexception details.
obviously has relationships beingness constructed here, don't know i'm doing wrong.
can recommend solution and/or explain problem here?
thanks in advance!
edit
here's screenshot of construction genergated. pkey field added loganbaseobject<t>
:
i need larn include absolutely info tables.
i had datetime columns in user table wasn't supplying values in seed
method. see here:
public class user : loganbaseobject<user> { public string username { get; set; } public string emailaddress { get; set; } public string password { get; set; } public string biography { get; set; } public virtual role role { get; set; } public bool passwordreset { get; set; } public string passwordresetkey { get; set; } public datetime resetexpiry { get; set; } public string createdby { get; set; } public datetime createdate { get; set; } }
i still don't why missing values produce error foreign keys, supplying values in seed method fixed problem.
c# entity-framework code-first
No comments:
Post a Comment