c# - Trying to get arround "Multiplicity constraint violated." / SaveChanges is Changing Values -
i'm trying overcome "multiplicity constraint violated." error. have created simple / contrived example demonstrate issue. in example, have task
, has collection of sub-tasks , task
can sub-task of 1 or more task
s. want able order sub-tasks. i'm open other suggestions on how have many-to-many relationship keeps track of order.
there 3 test bellow have different problems. 1 find interesting third test orderedtask
created , values correct until value taskid
changed someplace inside of 'context.savechanges()'
solution on git: https://github.com/jrswenson/orderedmanytomany
public class task { private icollection<orderedtask> subtasks; public task() { subtasks = new list<orderedtask>(); } public int id { get; set; } public string description { get; set; } public virtual user assigneduser { get; set; } public int? assigneduserid { get; set; } [inverseproperty("parent")] public virtual icollection<orderedtask> subtasks { { return subtasks; } set { subtasks = value; } } } public class orderedtask { public virtual task parent { get; set; } [key, column(order = 1), foreignkey("parent")] public int parentid { get; set; } public virtual task task { get; set; } [key, column(order = 2), foreignkey("task")] public int taskid { get; set; } public int order { get; set; } }
in unit test:
[testclass] public class tasktest { [testmethod] public void createtasks() { var context = new context(); var task = context.tasks.firstordefault(i => i.description == "lev1") ?? new task { description = "lev1" }; if (context.tasks.any(i => i.id == task.id) == false) context.tasks.add(task); var sub1 = context.tasks.firstordefault(i => i.description == "lev2-1") ?? new task { description = "lev2-1" }; if (context.tasks.any(i => i.id == sub1.id) == false) context.tasks.add(sub1); context.savechanges(); } //this throw "multiplicity constraint violated" error [testmethod] public void insertsubtasks() { var context = new context(); var task = context.tasks.firstordefault(i => i.description == "lev1"); assert.isnotnull(task); var sub1 = context.tasks.firstordefault(i => i.description == "lev2-1"); assert.isnotnull(sub1); if (task.subtasks.any(i => i.taskid == sub1.id) == false) { var ot = new orderedtask { parent = task, task = sub1, order = task.subtasks.count + 1 }; task.subtasks.add(ot); } context.savechanges(); } //this doesn't throw exception. //the orderedtask added database , //the table has correct values. //unfortunately, if ran second time, //task.subtasks empty , causes duplicate key error. [testmethod] public void insertsubtasks2() { var context = new context(); var task = context.tasks.firstordefault(i => i.description == "lev1"); assert.isnotnull(task); var sub1 = context.tasks.firstordefault(i => i.description == "lev2-1"); assert.isnotnull(sub1); if (task.subtasks.any(i => i.taskid == sub1.id) == false) { var ot = new orderedtask { parent = task, task = sub1, order = task.subtasks.count + 1 }; context.orderedtasks.add(ot); } context.savechanges(); } //this doesn't throw exception first time, on //second time. //the orderedtask created , has correct values after //added task.subtasks, somewhere in context.savechanges value //for parentid , taskid both set same value of parentid. //second time test ran task.subtasks has value (unlike test above) //, values not correct. [testmethod] public void insertsubtasks3() { var context = new context(); var task = context.tasks.firstordefault(i => i.description == "lev1"); assert.isnotnull(task); var sub1 = context.tasks.firstordefault(i => i.description == "lev2-1"); assert.isnotnull(sub1); if (task.subtasks.any(i => i.taskid == sub1.id) == false) { var ot = new orderedtask { parent = task, task = sub1, order = task.subtasks.count + 1 }; context.orderedtasks.add(ot); task.subtasks.add(ot); } context.savechanges(); } }
Comments
Post a Comment