views:

582

answers:

2

Hello.

I'm building ASP.NET MVC2 application, and using Entity Framework as ORM. I am having troubles updating object in the database. Every time I try entity.SaveChanges(), EF inserts new line in the table, regardless of do I want update, or insert to be done. I tried attaching (like in this next example) object to entity, but then I got

{"An object with a null EntityKey value cannot be attached to an object context."}

Here's my simple function for inserts and updates (it's not really about vehicles, but it's simpler to explain like this, although I don't think that this effects answers at all)...

        public static void InsertOrUpdateCar(this Vehicles entity, Cars car)
    {
        if (car.Id == 0 || car.Id == null)
        {
            entity.Cars.AddObject(car);
        }
        else
        {
            entity.Attach(car);
        }
        entitet.SaveChanges();
    }

I even tried using AttachTo("Cars", car), but I got the same exception.

Anyone has experience with this?

+2  A: 

I can give you a rough guide, but your code above doesn't give me a bunch to work with.

You would want to do something like:

using(Entities dataModel = new Entities())
{
    if(car.Id == 0 || car.Id == null)
    {
        dataModel.AddToCars(car); /* There should be a generated method similar to 
        this that just takes a car object minus the Primary Key */
    }
    else
    {
        var selectedCar = dataModel.Cars.Where(x => x.Id == car.Id).FirstOrDefault();

        if(selectedCar != null)
        {
            selectedCar.Name == car.Name; 
            // Continue updating your car stuff
        }
    }

    dataModel.SaveChanges();
}
Tejs
Well, I was thinking of this approach before, but it seems like an "workaround", and I'd like to use some nicer, more conventional method. However, your answer is useful. Thanks, here's vote up.
Eedoh
I tried this workaround and failed again with the exception message:"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."
Eedoh
This sounds like an issue with how your schema is defined, and how the EF Data Model generator produced code from your schema. I'd make sure that all your relationships are well defined in the edmx and that when you are adding / updating the entity in the dataModel that each of those relationships has the appropriate data. This is the dark side of EF.
Tejs
+1  A: 

If you are updating an existing record then you should have the EntityKey within the object instance you are providing to your InsertOrUpdate method. Go back to your code and see if you can locate where this is getting lost. I'd suspect you are presenting the user with a form to update this object and then mapping the response fields back into a Car object but you aren't passing the EntityKey with it (you probably don't want to display it to the user).

What you'd need to do is to include the Key in the form using an input type of 'hidden'. You can use the Html helper Html.Hidden("Key field name", key field value) to ensure it's passed to the user form and then back to your Post code.

Lazarus
Yes, I have strongly typed view and I have HttpPost method that gets a Car object from that form. PK is there and it's correct (I use hidden field already), but EntityKey is null. I can't figure out why it's lost, nor where :S
Eedoh
Forgot to update... This was the problem. It's needed to make changes and save on the object with same entity key.
Eedoh