Francis Ducharme
Francis Ducharme

Reputation: 4987

context.SaveChanges() not updating

So I added a BIT column to a table and now I need to make sure it is properly handled by the associated Windows Entity Framework class module.

Here's what I added in Order_Header.cs:

     //New added column...
    public virtual bool REPORT_UPLOADED
    {
        get;
        set;
    }

But the file looks like it was auto-generated by some tool... If so which ? Should I just rerun that tool for the new column to be taken into account ? Anyways.

Now I went in the class module which implements the Save, Update, Select, etc. function for the entity:

    public override int Save(Entities.Master.Order_Header type)
    {
        try
        {
            using (var context = new Master_Data_Container(ConfigurationManager.MasterDataConnection()))
            {
                context.Order_Header.AddObject(type);

                return context.SaveChanges();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public override int Update(Entities.Master.Order_Header type)
    {
        try
        {
            using (var context = new Master_Data_Container(ConfigurationManager.MasterDataConnection()))
            {
                var orderHeader = context.Order_Header.First(n => n.ID == type.ID);

                orderHeader.ADR_FACT = type.ADR_FACT;
                orderHeader.ADR_RAPPORT = type.ADR_RAPPORT;                 
                orderHeader.REPORT_UPLOADED = type.REPORT_UPLOADED;
                orderHeader.STATUT = type.STATUT;
                orderHeader.TELEPHONE = type.TELEPHONE;
                orderHeader.TYPE_COMMANDE = type.TYPE_COMMANDE;
                orderHeader.TYPE_EXAMEN = type.TYPE_EXAMEN;

                return context.SaveChanges();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

I am not very familiar with the Entity Framework so I tried mimicking what was already done for another entity I have in this project which works fine.

The update seems to work fine, except for REPORT_UPLOADED which never updates. an SQL Profiler trace shows that the new property/column is being ignored :(

                        orderHeader.REPORT_UPLOADED = true;
                        orderHeader.POSTE = "WHATEVER";
                        int result = new MsDt_OrderHeader().Update(orderHeader);

Is there anything I'm missing ?

Upvotes: 0

Views: 5247

Answers (3)

Lee O.
Lee O.

Reputation: 3312

You shouldn't be adding code to auto generated files. You would need to make it a partial class and put all of your custom code into another file. The real issue though sounds like it is as Mark suggested, none of the metadata describing the mapping of your conceptual model to the storage model was updated.

In Visual Studio, right click the .edmx file and select "Update Model from Database". If you are using T4 templates to customize your classes, you'll have to choose the template and select "Run Custom Tool".

Upvotes: 2

Mark Sowul
Mark Sowul

Reputation: 10600

If you just added the property manually, it probably doesn't have the metadata describing how to save it in the database. I don't have the EF docs handy, but in Linq to SQL there is a [Column] attribute. I assume EF has something similar.

Upvotes: 0

Luis
Luis

Reputation: 5914

In order to update you need to tell the the context that you're gonna update the model before you save the changes:

context.Entry(Order_Header).State = EntityState.Modified;
return context.SaveChanges();

Upvotes: 2

Related Questions