Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have class TaskStatus, that has collection of TaskTypes
public ICollection<tasktype> TaskTypes { get; set; }

And I have class TaskType, that has collection of TaskStatuses
public ICollection<taskstatus> TaskStatuses { get; set; }

So TaskStatus and TaskType are many to many related.
In DB I have table LinkTaskTypesToStatus with only 2 fields: FK_TaskStatus and FK_TaskType.

Using fluent API I've written:
modelBuilder.Entity<TaskStatus>().HasMany(t => t.TaskTypes).WithMany(t => t.TaskStatuses).Map(mc => { mc.ToTable("LinkTaskTypesToStatus"); mc.MapLeftKey("FK_TaskStatus"); mc.MapRightKey("FK_TaskType"); });

modelBuilder.Entity<TaskType>().HasMany(t => t.TaskStatuses).WithMany(t => t.TaskTypes).Map(mc => { mc.ToTable("LinkTaskTypesToStatus"); mc.MapLeftKey("FK_TaskType"); mc.MapRightKey("FK_TaskStatus"); });

But when I try to link task type with task status using
TaskTypes.Instance.PrintTask.TaskStatuses.Add(TaskStatuses.Instance.PrintTaskGeneratingTaskStatus);
context.SaveChanges();

it saves nothing. The only way to make a record to LinkTaskTypesToStatus is to use stored procedure
context.Database.ExecuteSqlCommand("EXECUTE AddLinkTaskTypesToStatus @TaskTypeId={0}, @TaskStatusId={1} ", TaskTypes.Instance.PrintTaskId, TaskStatuses.Instance.PrintTaskGeneratingTaskStatusId);

After it create record in LinkTaskTypesToStatus, but TaskTypes.Instance.PrintTask.TaskStatuses.Count=0 instead of 1. So it doesn't see new link in DB.

Could you help me is there is a way in Fluent API to achieve this?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900