c# - Entity framework and ASP.NET Identity -
using asp.net mvc 5 , entity framework 6, trying save new record package
database. have following model entities:
user.cs (applicationuser
in default template)
public class user : identityuser { private icollection<package> packages; public user() { this.packages = new hashset<package>(); } public virtual icollection<package> packages { { return this.packages; } set { this.packages = value; } } ... }
package.cs
public class package { private icollection<user> maintainers; public package() { this.maintainers = new hashset<user>(); } [required] public virtual icollection<user> maintainers { { return this.maintainers; } set { this.maintainers = value; } } ... }
i have following dbcontext class:
public class applicationdbcontext : identitydbcontext<user>, iapplicationdbcontext { public applicationdbcontext() : base("defaultconnection", throwifv1schema: false) { } public virtual idbset<package> packages { get; set; } ... public static applicationdbcontextcreate() { return new applicationdbcontext(); } }
i have packageservice.cs (similar service pattern):
public class packagesservice : ipackagesservice { private readonly irepository<package> packages; public packagesservice(irepository<package> packages) { this.packages = packages; } public package create(ilist<user> maintainers) { var newpackage = new package { maintainers = maintainers }; this.packages.add(newpackage); this.packages.savechanges(); return newpackage; } }
i have standard implementation of irepository genericrepository: source code here.
now problem i'm having when create()
method called entity framework throws following exception:
an exception of type 'system.invalidoperationexception' occurred in entityframework.dll not handled in user code
additional information: entity object cannot referenced multiple instances of ientitychangetracker.
i understand problem package
entity change-tracked 2 different dbcontexts, use same applicationdbcontext
through entire application. moreover, here's how inject (using ninject):
kernel.bind<dbcontext>().to<applicationdbcontext>().inrequestscope(); kernel.bind(typeof(irepository<>)).to(typeof(genericrepository<>)); kernel.bind<iuserstore<user>>().to<userstore<user>>().withconstructorargument("context", kernel.get<dbcontext>()); kernel.bind<usermanager<user>>().toself();
this part confuses me most. not bind context ninject, wherever use i.e usermanager
, through owin instance, creates create() static method of same dbcontext. how there 2 dbcontext instances?
i've spent whole day debugging , can't understand why it's happening. in advance.
i'm not sure whether problem di bindings , ninject in particular, managed find workaround. since apparently had 2 instances of dbcontext, instead of using records first 1 mapped many-to-many relationship primary keys only. along following lines:
if (maintainerids != null && maintainerids.count > 0) { foreach (string maintainerid in maintainerids) { var maintainer = new user() { id = maintainerid }; users.attach(maintainer); newpackage.maintainers.add(maintainer); } }
this doesn't require dbcontext long ids match.
Comments
Post a Comment