Let’s say I create a new Entity and Save it as follows:
UserReport report = new UserReport() {//set the props}
manager.SaveUserReport(report)
Public UserReport SaveUserReport(UserReport report)
{
using(var context = new ReportDatabase())
{
context.UserReports.AdObject(report);
context.SaveChanges();
}
return report;
}
so far so good
I then read back the saved Report
savedReport = manager.GetUserReports(new int[] {report.Id}).FirstOrDefault();
Public List<UserReport> GetUserReports(IEnumerable<int> reportIds)
{
using (var context = new ReportDatabase())
{
var reports = from UserReport in context.UserReports
where reportIds.Contains(userReport.Id)
select userReport;
return visibleReports.ToList();
}
}
savedReport is now an attached UserReport
The UserReport object has a collection of Columns attached to it.
I want to replace the set of Columns attached with another set (that already exist in the database).
savedReport.Columns = newColumnCollection
This fails with the error “The property Columns” on type UserReport_etc’ cannot be set because the collection is already set to an EntityCollection”
I’ve looked at this article: the problem is the same, but I cannot use that solution.
What is the correct way to tackle this?
OK – looks like it’s just a case of setting the non-navigation properties to not be virtual.
That is one hell of a weird situation, given that the behaviour that is modified is of properties that remain as virtual.