c# - Error when creating a new record in asp.net Razor Engine -
i have 2 tables in database. first 1 mystudent , other 1 mycourse. use entityframework of .net , in visual studio created model called "bestmodel". here classes;
mystudent.cs
namespace tt.models { using system; using system.collections.generic; using system.componentmodel.dataannotations.schema; public partial class mystudent { [system.diagnostics.codeanalysis.suppressmessage("microsoft.usage", "ca2214:donotcalloverridablemethodsinconstructors")] public mystudent() { this.mycourse = new hashset<mycourse>(); } public int id { get; set; } public string name { get; set; } [foreignkey("mycourse")] public nullable<int> courseid { get; set; } [system.diagnostics.codeanalysis.suppressmessage("microsoft.usage", "ca2227:collectionpropertiesshouldbereadonly")] public virtual icollection<mycourse> mycourse { get; set; } } }
mycourse.cs
namespace tt.models { using system; using system.collections.generic; using system.componentmodel.dataannotations.schema; public partial class mycourse { [system.diagnostics.codeanalysis.suppressmessage("microsoft.usage", "ca2214:donotcalloverridablemethodsinconstructors")] public mycourse() { this.mystudent = new hashset<mystudent>(); } public int id { get; set; } public string title { get; set; } [foreignkey("mystudent")] public nullable<int> studentid { get; set; } [system.diagnostics.codeanalysis.suppressmessage("microsoft.usage", "ca2227:collectionpropertiesshouldbereadonly")] public virtual icollection<mystudent> mystudent { get; set; } } }
here table definitions;
create table [dbo].[mystudent] ( [id] int not null, [name] nvarchar (50) null, [courseid] int null, foreign key (courseid) references mycourse(id), primary key clustered ([id] asc) ); create table [dbo].[mycourse] ( [id] int not null, [title] nvarchar (50) null, [studentid] int null, foreign key (studentid) references mystudent(id), primary key clustered ([id] asc), ); create table [dbo].[mystudentcourses] ( [courseid] int not null, [studentid] int not null, foreign key (courseid) references mycourse(id), foreign key (studentid) references mystudent(id) );
and here create function;
[httppost] [validateantiforgerytoken] public actionresult create([bind(include = "id,title")] mycourse mycourse) { if (modelstate.isvalid) { mystudent st = new mystudent(); st.id = 10; st.courseid = 1; st.name = "sadasd".tostring(); mycourse.mystudent.add(st); // when line commented fine.. . db.mycourse.add(mycourse); db.savechanges(); return redirecttoaction("index"); } return view(mycourse); }
i specified line of interest problem above. when want create new record gives me "an error occurred while updating entries. see inner exception details." when disabled line "mycourse.mystudent.add(st);" fine. think problem relationship between these 2 tables , foreign keys. please me . ..
edit
the relationship between classes many many . . .
edit2
innerexception that;
system.data.entity.core.updateexception: error occurred while updating entries. see inner exception details. ---> system.data.sqlclient.sqlexception: insert statement conflicted foreign key constraint "fk__mystudent__cours__35bcfe0a". conflict occurred in database "mydb", table "dbo.mycourse", column 'id'. statement has been terminated. @ system.data.sqlclient.sqlconnection.onerror(sqlexception exception, boolean breakconnection, action1 wrapcloseinaction) @ system.data.sqlclient.sqlinternalconnection.onerror(sqlexception exception, boolean breakconnection, action
1 wrapcloseinaction) @ system.data.sqlclient.tdsparser.throwexceptionandwarning(tdsparserstateobject stateobj, boolean callerhasconnectionlock, boolean asyncclose) @ system.data.sqlclient.tdsparser.tryrun(runbehavior runbehavior, sqlcommand cmdhandler, sqldatareader datastream, bulkcopysimpleresultset bulkcopyhandler, tdsparserstateobject stateobj, boolean& dataready) @ system.data.sqlclient.sqlcommand.finishexecutereader(sqldatareader ds, runbehavior runbehavior, string resetoptionsstring, boolean isinternal, boolean fordescribeparameterencryption) @ system.data.sqlclient.sqlcommand.runexecutereadertds(commandbehavior cmdbehavior, runbehavior runbehavior, boolean returnstream, boolean async, int32 timeout, task& task, boolean asyncwrite, boolean inretry, sqldatareader ds, boolean describeparameterencryptionrequest) @ system.data.sqlclient.sqlcommand.runexecutereader(commandbehavior cmdbehavior, runbehavior runbehavior, boolean returnstream, string method, taskcompletionsource1 completion, int32 timeout, task& task, boolean& usedcache, boolean asyncwrite, boolean inretry) @ system.data.sqlclient.sqlcommand.internalexecutenonquery(taskcompletionsource
1 completion, string methodname, boolean sendtopipe, int32 timeout, boolean& usedcache, boolean asyncwrite, boolean inretry) @ system.data.sqlclient.sqlcommand.executenonquery() @ system.data.entity.infrastructure.interception.dbcommanddispatcher.b__0(dbcommand t, dbcommandinterceptioncontext1 c) @ system.data.entity.infrastructure.interception.internaldispatcher
1.dispatch[ttarget,tinterceptioncontext,tresult](ttarget target, func3 operation, tinterceptioncontext interceptioncontext, action
3 executing, action3 executed) @ system.data.entity.infrastructure.interception.dbcommanddispatcher.nonquery(dbcommand command, dbcommandinterceptioncontext interceptioncontext) @ system.data.entity.internal.interceptabledbcommand.executenonquery() @ system.data.entity.core.mapping.update.internal.dynamicupdatecommand.execute(dictionary
2 identifiervalues, list1 generatedvalues) @ system.data.entity.core.mapping.update.internal.updatetranslator.update() --- end of inner exception stack trace --- @ system.data.entity.core.mapping.update.internal.updatetranslator.update() @ system.data.entity.core.entityclient.internal.entityadapter.b__2(updatetranslator ut) @ system.data.entity.core.entityclient.internal.entityadapter.update[t](t nochangesresult, func
2 updatefunction) @ system.data.entity.core.entityclient.internal.entityadapter.update() @ system.data.entity.core.objects.objectcontext.b__35() @ system.data.entity.core.objects.objectcontext.executeintransaction[t](func1 func, idbexecutionstrategy executionstrategy, boolean startlocaltransaction, boolean releaseconnectiononsuccess) @ system.data.entity.core.objects.objectcontext.savechangestostore(saveoptions options, idbexecutionstrategy executionstrategy, boolean startlocaltransaction) @ system.data.entity.core.objects.objectcontext.<>c__displayclass2a.b__27() @ system.data.entity.sqlserver.defaultsqlexecutionstrategy.execute[tresult](func
1 operation) @ system.data.entity.core.objects.objectcontext.savechangesinternal(saveoptions options, boolean executeinexistingtransaction) @ system.data.entity.core.objects.objectcontext.savechanges(saveoptions options) @ system.data.entity.internal.internalcontext.savechanges()
by default, join table uses composite key composed of foreign keys of 2 sides of relationship. error you're getting means there's entry particular combination you're trying save. right now, looks setting every new student's id 10, doesn't make sense in first place. definitely, once you've run once, further attempts generate error, though, because there have been association between course , student id of 10.
Comments
Post a Comment