I'm using EF4 with POCO and the code below is what I have to update a license. I only pasted here the section of the code that is relevant to the problem, which is adding LicenseDetails.
The problem is that for each LicenseDetail that are inserted, EF also unexpectadly adds rows to the "Item" lookup table. Why?!
The relationship of both tables defined in the database (SQLServer 2008) is shown below and I do Database first, therefore EF generates the entities' relationship based on this.
ALTER TABLE [dbo].[LicenseDetail]
WITH CHECK ADD CONSTRAINT [FK_LicenseDetail_Item] FOREIGN KEY([ItemId])
REFERENCES [dbo].[Item] ([Id])
GO
ALTER TABLE [dbo].[LicenseDetail] CHECK CONSTRAINT [FK_LicenseDetail_Item]
GO
the update method:
public static void UpdateLicense(License license)
{
using (ALISEntities context = new ALISEntities())
{
context.ContextOptions.ProxyCreationEnabled = true;
var storedLicense =
context.Licenses.Include("LicenseDetails")
.Where(o => o.Id == license.Id).SingleOrDefault();
//////////////////////////////////////////////////////////////
// license details to add
List<LicenseDetail> toAdd = new List<LicenseDetail>();
foreach (LicenseDetail ld in license.LicenseDetails)
{
if (storedLicense.LicenseDetails
.Where(d => d.ItemId == ld.ItemId).Count() == 0)
{
toAdd.Add(ld);
}
}
toAdd.ForEach(i => storedLicense.LicenseDetails.Add(i));
context.SaveChanges();
}
}
When you add a new LicenseDetails to context you also add Items which are referenced by those LicenseDetails. Because context doesn't know that Items already exists in the database it adds them. You need to tell context that Items are already in the database by calling context.Items.Attach(licenseDetail.Item).
You might also try using
context.Licenses.Include("LicenseDetails").Find(license.Id);
instead of
context.Licenses.Include("LicenseDetails")
.Where(o => o.Id == license.Id).SingleOrDefault();
and there is no need to use toAdd list at all - just keep adding licenseDetails in the first foreach loop.
I ended up having to issue context.ObjectStateManager.ChangeObjectState(d.Item, EntityState.Unchanged) for each LicenseDetail added. That solved the problem.
Related
I am getting this error after click Save (update) my form:
The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
Here is my controller (case "Save" in swich couse problem):
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(UserModel userModel, string act = null, int idx = 0)
{
using (var dbContext = new userDbEntities())
{
if (userModel.User == null)
{
userModel.User = new UsersTable();
}
var newUser = userModel.User.userID == 0;
userModel.CustomTypes = dbContext.CustomTypes.ToList();
switch (act)
{
case "addcustom":
userModel.User.CustomerTables.Add(new CustomerTable
{
CustomType = new CustomType(),
UsersTable = userModel.User
});
break;
case "deletecustom":
userModel.User.CustomerTables.RemoveAt(idx);
break;
case "save":
foreach (var customer in userModel.User.CustomerTables)
{
customer.CustomType = dbContext.CustomTypes.Find(customer.CustomType.Id_NewCustomerType);
}
var dbUser = dbContext.UsersTables.Find(userModel.User.userID);
dbUser.TimeZoneId = userModel.User.TimeZoneId;
foreach (var custom in userModel.User.CustomerTables)
{
if (custom.CustomerID == 0)
dbUser.CustomerTables.Add(custom);
}
foreach (var custom in dbUser.CustomerTables.ToList())
{
var modelCustom =
userModel.User.CustomerTables.FirstOrDefault(o => o.CustomerID == custom.CustomerID);
if (modelCustom != null) //update it
{
custom.CustomType =
dbContext.CustomTypes.Find(modelCustom.CustomType.Id_NewCustomerType);
}
if (userModel.User.CustomerTables.All(o => o.CustomerID != custom.CustomerID))
dbUser.CustomerTables.Remove(custom);
}
dbContext.SaveChanges();
break;
} // end switch statements
return View("Edit", userModel);
}
}
Any idea what is wrong...
try something like following.
foreach (var child in modifiedParent.ChildItems)
{
context.Childs.Attach(child);
context.Entry(child).State = EntityState.Modified;
}
context.SaveChanges();
See the following link.
http://social.msdn.microsoft.com/Forums/en-US/1833117c-7a93-4b69-a133-b7fd764db810/the-operation-failed-the-relationship-could-not-be-changed-because-one-or-more-of-the-foreignkey?forum=adodotnetentityframework
There is an easier way to solve this problem.
Actually this is because you have one to many relationship. When you want to delete 1 Side, you have two scenarios; You can choose cascade delete, or you can choose none.
If you select first option it will delete all the many side entities when you delete the 1 side.
You can set this option in the Entity Diagram. You Only got to select the relationship and set "End1 OnDelete" property to Cascade.
I was experiencing this message, it turned out that I was marking the object as Modified after I had removed it from it's collection instead of deleting it.
For example you don't want to call
context.Entry(child).State = EntityState.Modified;
if in fact you are trying to delete the child.
instead, as well as removing the child from the collection try something like context.DeleteObject(child)
I haven't got the exact code because my experience was with Dev Express XAF ObjectSpace which can be cast to ObjectContext using ((EFObjectSpace)objectSpace).ObjectContext;
I experienced this problem, it's complicated to explain (so please forgive the analogy), the good news is that the solution is very simple.
Assume I have a record with a one to many relationship, which also has a relationship. (A "Car" has a number of "People"; "People" have a number of "Children"). The error occurrs when removing people from car. Doing a db.Cars.Remove(person);. Caused an unrelated error. To resolve that issue I did a db.Cars.Persons.RemoveRange(children); <--- After this I experienced the error as stated in this question. To resolve this remove the child's children from the database (not the "Parent" but the database iteself) db.Children.RemoveRange(childrenOfPeople);.
This is the cleanest solution to the problem using entity framework correctly without setting the state of records. Hope that helps someone.
Is there an equivalent of Rails ActiveRecord::Callbacks in ASP MVC?
http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
I'm in a situation where we are not using identities for our primary key. We do this for reasons specific to our DB sharding design. Because of this we have a lookup table to find the next ID for a specific table. I'd like to automatically get this value and set it in an abstract class whenever a model is created/updated and before it is saved. I also need to update the lookup table with an incremented 'nextID' after the save is successful.
I'm open to other solutions on how to do this without callbacks as well.
So you need the callback just to increment ID in the lookup table? AFAIK there is no equivalent in ASP.NET, may be you could try with Async Controllers (http://msdn.microsoft.com/en-us/library/ee728598%28v=vs.100%29.aspx) and wait for a state change from the successful save, but I would prefer use a service specifically for this like Snowflake (https://github.com/twitter/snowflake/).
I found a solution using overrides as opposed to callbacks. It's my hope that ASP mvc adds support for callbacks as the framework continues to mature because callbacks allow for cleaner code by allowing the OnSave event to exist in the model[s] that the event is concerned with rather than the centralized DbContext class (separation of concerns).
Solution:
The SaveChanges method can be overridden in the Context Class (Entity Framework Power Tools creates the Context class is the 'Models' directory).
public override int SaveChanges()
{
// create a cache for id values in case their are multiple added entries in the dbcontext for the same entitytype
Dictionary<string, UniqueID> idCache = new Dictionary<string, UniqueID>();
IEnumerable<DbEntityEntry> changes = this.ChangeTracker.Entries();
foreach (var entry in changes)
{
//check if this is a new row (do nothing if its only a row update because there is no id change)
if (entry.State == System.Data.EntityState.Added)
{
//determine the table name and ID field (by convention)
string tableName = entry.Entity.GetType().Name;
string idField = entry.Entity.GetType().Name + "ID";
UniqueID id = null;
//if we've already looked this up, then use the cache
if (idCache.ContainsKey(tableName))
{
id = idCache[tableName];
}
//if we havn't looked this up before get it and add it to the cache
else
{
id = this.UniqueIDs.Find(tableName, idField);
//if it doesn't already exist in the lookup table create a new row
if (id == null)
{
id = new UniqueID(tableName, idField, 1);
// since this is a new entry add it
this.UniqueIDs.Add(id);
}
else
{
// set the state to modified
this.Entry(id).State = System.Data.EntityState.Modified;
}
}
entry.CurrentValues[tableName + "ID"] = id.NextID;
id.NextID = id.NextID + 1;
}
}
return base.SaveChanges();
}
I'm developing a website using MVC4 with EF code first approach.
I have some problems with deleting the children in entities with one to many relations.
Edit to clearify: In my Edit view, I add/remove/update existing childen in the childrens collection on the parent, add/remove is done using javascript. When I receive the updated parent in the post request in the controller method, I want to sync/update the parent and child entities in the database.
The parent object is in a detached state when updated in the view. So, when I attach the parent again, I want it to do all the updates that have been done during detached state.
The entity relations are set up so that when removing a child entity from the parent collection, the child entity is also deleted from the child table (cascade delete sort of?), and this works when in attached state.
However, when attching the parent and saving the changes, only added/updated children are added/modified in the database. But removed children from the parent collection are not deleted in the database (which I want them to be).
How can this be solved??
The entities are:
class Parent
{
public virtual ICollection<Child> Children { get; set; }
}
class Child
{
public string Text { get; set; }
}
This works and will remove child from database:
void RemoveChildFromCollection()
{
// get the first parent and remove the first child in collection
var context = new DatabaseContext();
var parent = context.Parents.First();
parent.Children.Remove(parent.Children.First());
context.SaveChanges();
}
ControllerMethod: This does not work as above, removed children are not removed from the childrens table
public ActionResult Edit(Parent parent)
{
var context = new DatabaseContext();
context.Entry(parent).State = System.Data.EntityState.Modified;
context.SaveChanges();
return View();
}
The modelbuilder are setup to delete child entity from child table when removing them from parent collection
// Use Identifying relation. Define complex key for ChildObject containing both Id and
ParentObjectId
modelBuilder.Entity<Child>()
.HasKey(c => new {c.ChildID, c.ParentID});
// Because defining such key will remove default convention for auto incremented Id you must redefine it manually
modelBuilder.Entity<Child>()
.Property(c => c.ChildID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
// Set cascade delete
modelBuilder.Entity<Parent>()
.HasMany(p => p.Children)
.WithRequired()
.HasForeignKey(c => c.ParentID)
.WillCascadeOnDelete();
Cascade delete only deletes child entities, when the parent entity is also removed, as you've noted, not when you sever the relationship.
You can override your SaveChanges() in your Context to clean up your orphaned Child entities like this:
public override int SaveChanges()
{
Children
.Local
.Where(c => c.Parent == null)
.ToList()
.ForEach(child => Children.Remove(child));
return base.SaveChanges();
}
This blog post has more info on handling orphaned entities.
Your entities - parent and children - are in a detached state when you do the modification in the view. So, EF couldn't track those changes. When you attach the object graph to the context - by setting the parent's state to Modified - EF takes this attached object graph as the current state and doesn't know anything about the removal of the child that happened during the detached phase in the view.
To solve the problem you must load the current object graph - parent including children - from the database, compare it with the object graph from your view and merge the changes into the loaded graph. Then save the changes. There can be several possible changes:
The parent's scalar properties could have been changed
The children's scalar properties can been changed
A new child could have been added to the Children collection
A child could have been removed from the Children collection
Your current code - setting the parent's state to Modified - will only handle the first case correctly but not the other three cases.
To deal with all four cases you need to follow the procedure described above. An example how to do that is shown here (see the Edit section in that answer).
The code in your Edit post action would then look like this:
public ActionResult Edit(Parent parent)
{
using (var context = new DatabaseContext())
{
var parentInDb = context.Parents
.Include(p => p.Children)
.Single(p => p.ParentId == parent.ParentId);
context.Entry(parentInDb).CurrentValues.SetValues(parent);
foreach (var childInDb in parentInDb.Children.ToList())
if (!parent.Children.Any(c =>
c.ChildId == childInDb.ChildId &&
c.ParentId == childInDb.ParentId)) // or == parent.ParentId
context.Children.Remove(childInDb);
// here
// parentInDb.Children.Remove(childInDb);
// should work too because you have an identifying relationship
foreach (var child in parent.Children)
{
var childInDb = parentInDb.Children.SingleOrDefault(c =>
c.ChildId == child.ChildId &&
c.ParentId == child.ParentId); // or == parent.ParentId
if (childInDb != null)
context.Entry(childInDb).CurrentValues.SetValues(child);
else
parentInDb.Children.Add(child);
}
context.SaveChanges();
return View();
}
}
When I update my model I get an error on a child relation which I also try to update.
My model, say Order has a releationship with OrderItem. In my view I have the details of the order together with an editortemplate for the orderitems. When I update the data the link to Order is null but the orderid is filled, so it should be able to link it, TryUpdateModel returns true, the save however fails with:
InvalidOperationException: The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.]
My update method:
public ActionResult ChangeOrder(Order model)
{
var order = this.orderRepository.GetOrder(model.OrderId);
if (ModelState.IsValid)
{
var success = this.TryUpdateModel(order);
}
this.orderRepository.Save();
return this.View(order);
}
I tried all solutions I saw on SO and other sources, none succeeded.
I use .Net MVC 3, EF 4.3.1 together with DBContext.
There are a number of code smells here, which I'll try to be elegant with when correcting :)
I can only assume that "Order" is your EF entity? If so, I would highly recommend keeping it separate from the view by creating a view model for your form and copying the data in to it. Your view model should really only contain properties that your form will be using or manipulating.
I also presume orderRepository.GetOrder() is a data layer call that retrieves an order from a data store?
You are also declaring potentially unused variables. "var order =" will be loaded even if your model is invalid, and "var success =" is never used.
TryUpdateModel and UpdateModel aren't very robust for real-world programming. I'm not entirely convinced they should be there at all, if I'm honest. I generally use a more abstracted approach, such as the service / factory pattern. It's more work, but gives you a lot more control.
In your case, I would recommend the following pattern. There's minimal abstraction, but it still gives you more control than using TryUpdateModel / UpdateModel:
public ActionResult ChangeOrder(OrderViewModel model) {
if(ModelState.IsValid) {
// Retrieve original order
var order = orderRepository.GetOrder(model.OrderId);
// Update primitive properties
order.Property1 = model.Property1;
order.Property2 = model.Property2;
order.Property3 = model.Property3;
order.Property4 = model.Property4;
// Update collections manually
order.Collection1 = model.Collection1.Select(x => new Collection1Item {
Prop1 = x.Prop1,
Prop2 = x.Prop2
});
try {
// Save to repository
orderRepository.SaveOrder(order);
} catch (Exception ex) {
ModelState.AddModelError("", ex.Message);
return View(model);
}
return RedirectToAction("SuccessAction");
}
return View(model);
}
Not ideal, but it should serve you a bit better...
I refer you to this post, which is similar.
I assume that the user can perform the following actions in your view:
Modify order (header) data
Delete an existing order item
Modify order item data
Add a new order item
To do a correct update of the changed object graph (order + list of order items) you need to deal with all four cases. TryUpdateModel won't be able to perform a correct update of the object graph in the database.
I write the following code directly using a context. You can abstract the use of the context away into your repository. Make sure that you use the same context instance in every repository that is involved in the following code.
public ActionResult ChangeOrder(Order model)
{
if (ModelState.IsValid)
{
// load the order from DB INCLUDING the current order items in the DB
var orderInDB = context.Orders.Include(o => o.OrderItems)
.Single(o => o.OrderId == model.OrderId);
// (1) Update modified order header properties
context.Entry(orderInDB).CurrentValues.SetValues(model);
// (2) Delete the order items from the DB
// that have been removed in the view
foreach (var item in orderInDB.OrderItems.ToList())
{
if (!model.OrderItems.Any(oi => oi.OrderItemId == item.OrderItemId))
context.OrderItems.Remove(item);
// Omitting this call "Remove from context/DB" causes
// the exception you are having
}
foreach (var item in model.OrderItems)
{
var orderItem = orderInDB.OrderItems
.SingleOrDefault(oi => oi.OrderItemId == item.OrderItemId);
if (orderItem != null)
{
// (3) Existing order item: Update modified item properties
context.Entry(orderItem).CurrentValues.SetValues(item);
}
else
{
// (4) New order item: Add it
orderInDB.OrderItems.Add(item);
}
}
context.SaveChanges();
return RedirectToAction("Index"); // or some other view
}
return View(model);
}
I am having trouble saving my entities after updating them. I can add new entities like this: add(student); but if I tried this:
if (ModelState.IsValid)
{
db.Entry(student).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("someView");
}
I get this error message:
System.Data.Entity.Infrastructure.DbUpdateConcurrencyException was unhandled by user code
Message=Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or
deleted since entities were loaded. Refresh ObjectStateManager
entries.
Here’s my controller method:
[HttpPost]
public ActionResult ClassAttendance(InstructorIndexData viewModel, FormCollection frmcol)
{
var instructorData = new InstructorIndexData();
string[] AllFstMNames = frmcol["item.Student.FirstMidName"].Split(',');
string[] AllLstNames = frmcol["item.Student.LastName"].Split(',');
string[] AllAddresses = frmcol["item.Student.Address"].Split(',');
string[] AllEnrollmentDates = frmcol["item.Student.EnrollmentDate"].Split(',');
//more of the same code…
var student = new Student();
var enrollment = new Enrollment();
for ( int i = 0; i < AllFstMNames.Count(); i++)
{
student.FirstMidName = AllFstMNames[i];
student.LastName = AllLstNames[i];
student.Address = AllAddresses[i];
student.EnrollmentDate = Convert.ToDateTime(AllEnrollmentDates[i]);
if (!string.IsNullOrEmpty(frmcol["item.Grade"]))
{
enrollment.Grade = Convert.ToInt32(AllGrades[i]);
}
enrollment.StudentID = Convert.ToInt32(AllStudentIds[i]);
enrollment.attendanceCode = Convert.ToInt32(AllAttendanceCodes[i]);
enrollment.classDays = AllclassDays[i];
enrollment.CourseID = Convert.ToInt32 (AllCourseIds[i]);
//update rows
}
if (ModelState.IsValid)
{
db.Entry(student).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("someView");
}
Can you help me with just being able to update values in the database?
While I was looking at the code here, my initial thought is that it doesn't seem quite right to have a for loop that updates the student and enrollment objects multiple times and then to have only one call to db.SaveChanges outside the loop. This is concerning because only the last iteration of the for loop will be applied when the data is saved to the database. (You have a comment to "update rows" at the end of the for loop - perhaps some code is missing or misplaced?)
Then, I started thinking about why it would be necessary to manually set the Entry(...).State property. Wouldn't the db automatically know that an object is modified and needs to be saved? That lead me to this question: Where is db defined? What technology stack is being used there?
Finally, after making an assumption that the db object might work something like the MS LINQ-to-SQL feature, I noticed that the the student object is newly instantiated before the for loop. This is fine for inserting new data, but if you are wanting to update existing data, I believe you need to first get a copy of the object from the database and then update the properties. This allows the db object to monitor the changes (again, assuming that it has this capability). (If this is not the case, then it leads me to wonder how the db will know which record in the database to update since you are not setting anything that appears to be a primary key, such as StudentId, on the student object in the loop.)