I’m making a database in EF4.1 Code First. I have a table, MedicalPlan, with a one-to-many relationship to a CoverageLevel. CoverageLevel primary key is incrementing. When I create the MedicalPlan I declare the coveragelevels and it creates those tables, like so:
medicalPlan.CoverageLevels = new List<CoverageLevel>();
medicalPlan.CoverageLevels.Add(new CoverageLevel() { Cost = 1200, Description = "single" });
medicalPlan.CoverageLevels.Add(new CoverageLevel() { Cost = 1500, Description = "spouse" });
medicalPlan.CoverageLevels.Add(new CoverageLevel() { Cost = 1100, Description = "family" });
I also have an update function in which I would update a medical plan. I would also like the functionality to update the MedicalPlan’s CoverageLevels. In pseudocode, something like:
in medicalPlan edit first item in CoverageLevels() { Cost = 1500 };
The kicker is that I don’t actually want to replace the CoverageLevel, as it has a unique auto-incrementing primary key, so if I create a new one it will have a different primary key than the original. Is there a way to do this in the way that I am attempting?
Once you have loaded a MedicalPlan from the database you should be able to work with it and any related entities using Linq or accessing directly via the property on MedicalPlan as if it were a normal collection of .NET objects.
Calling Save would persist back to the database.
E.g.
Or probably more likely..