c# - Entity Framework Exception at run time: Entities in participate in the relationship? -



c# - Entity Framework Exception at run time: Entities in participate in the relationship? -

while using code first approach when run project. during new user registration, entity framework throwing exception:

additional information: entities in 'applicationdbcontext.identityusers' participate in 'applicationuser_userdetails' relationship. 0 related 'applicationuser_userdetails_target' found. 1 'applicationuser_userdetails_target' expected.

the exception thrown:

var manager = new usermanager(); var user = new applicationuser() { username = username.text }; //this line @ time of creating user identityresult result = manager.create(user, password.text);

i created schema code:

public class applicationuser : identityuser { public nullable<datetime> dateofcreation { get; set; } public nullable<bool> isactive { get; set; } public nullable<datetime> lastlogin { get; set; } public userdetails userdetails { get; set; } public virtual icollection<tasksheetmanagement> tasksheet { get; set; } public virtual icollection<projects> projects { get; set; } } public class applicationdbcontext : identitydbcontext<applicationuser> { public applicationdbcontext() : base("defaultconnection") { } protected override void onmodelcreating(system.data.entity.dbmodelbuilder modelbuilder) { modelbuilder.entity<identityuser>().haskey(pk => pk.id).totable("users"); modelbuilder.entity<applicationuser>().haskey(pk=>pk.id).totable("users"); modelbuilder.entity<userdetails>().haskey(pk => pk.userdetailid).totable("userdetails"); modelbuilder.entity<applicationuser>().hasrequired(p => p.userdetails).withrequiredprincipal(c => c.applicationuser); modelbuilder.entity<applicationuser>() .property(t => t.username).isrequired() .hasmaxlength(14) .hascolumnannotation("index", new indexannotation(new indexattribute("u_username", 1) { isunique = true })); modelbuilder.entity<userdetails>() .property(t => t.email).isrequired() .hasmaxlength(25) .hascolumnannotation("index", new indexannotation(new indexattribute("u_email", 2) { isunique = true })); base.onmodelcreating(modelbuilder); } }

and userdetails is:

public class userdetails { public int userdetailid { get; set; } public string name { get; set; } public string email { get; set; } public string countryname { get; set; } public string gender { get; set; } public string nic { get; set; } public string residentaddress { get; set; } public string cellno { get; set; } public string phoneno { get; set; } public string religion { get; set; } public applicationuser applicationuser { get; set; } }

i couldn't find out exception related to. searched google , implemeted other solutions couldn't resolve. help please.

you've created 1:1 association, 1:1 associations not work way in entity framework.

1:1 associations must share same primary key. way you've defined 2 1:m associations, , conflict each other.

if want 1:1, have alter primary key userdetails id, , have map them like:

modelbuilder.entity<userdetails>().hasrequired(x => x.applicationuser) .withrequireddependent(x => x.userdetails);

c# asp.net entity-framework entity-framework-5 asp.net-identity

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -