0

I have the following code and I cannot achieve saving the changes. The parameter of my method is a string containing the RefCode of a product I want to modify in the database, then the query is pulling the BaseProduct that is supposed to be modified. (I tried to simplify the code and set it in English, so I have probably introduced some syntactic errors, but in my code in debug mode, I get all the info FROM the DB). Is there something wrong with the "select new" in the Linq query ?

public static void UpdateProduct(ViewProduct productToUpdate)
    {
        using (var context = new my_Entities())
        {
            var BaseProduct = (from prod in context.Product
                               where prod.Ref == productToUpdate.BaseProduct.RefPrd
                                      select new ViewBaseProduct
                                      {
                                          RefPrd = prod.Ref,
                                          DescrPrd = prod.DescrPrd,
                                          NormeCe = (bool)prod.NormeCE
                                      }).FirstOrDefault();

            if (BaseProduct != null)
            {
                //BaseProduct.NormeCe = false;
                BaseProduct = productToUpdate.BaseProduct;
                context.SaveChanges();
            }
        }
    }

3 Answers 3

2

But BaseProduct is a ViewBaseProduct object, is ViewBaseProduct a entity class? It seems it is a ViewModel class.

You have to get de Product entity, modify his fields and savechanges. It seems you only apply changes to the ViewModel class.

Try this:

public static void UpdateProduct(ViewProduct productToUpdate)
{
    using (var context = new my_Entities())
    {
        var BaseProduct = (from prod in context.Product
                           where prod.Ref == productToUpdate.BaseProduct.RefPrd)
                          .FirstOrDefault();

        if (BaseProduct != null)
        {
            //BaseProduct.NormeCe = false;
            BaseProduct.field1 = productToUpdate.BaseProduct.field1;
            BaseProduct.field2 = productToUpdate.BaseProduct.field2;

            //update the necesary fields
            //......
            context.SaveChanges();
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

you are right, ViewBaseProduct is a ViewModel class. I'll try what you propose it seems so obvious now...
that was it :) Thanks Carlos ! When I was doing a "select new ViewBaseProduct" in my Linq query, I was creating an object that was not "linked" to the Enity, so updating that object could not affect the datas in the database.
0

This won't work that way. You should use the CurrentValues.SetValues() method:

contexte.Entry(BaseProduct).CurrentValues.SetValues(productToUpdate.BaseProduct);

Comments

0

I think you have to Try this

public static void UpdateProduct(ViewProduct productToUpdate)
    {
        using (var contexte = new my_Entities())
        {
            var BaseProduct = (from prod in contexte.Product
                               where prod.Ref == productToUpdate.BaseProduct.RefPrd
                                      select new ViewBaseProduct
                                      {
                                          RefPrd = prod.Ref,
                                          DescrPrd = prod.DescrPrd,
                                          NormeCe = (bool)prod.NormeCE
                                      }).FirstOrDefault();

            if (BaseProduct != null)
            {
                BaseProduct.BaseProduct.RefPrd=productToUpdate.BaseProduct.RefPrd
                BaseProduct.BaseProduct.DescrPrd=productToUpdate.BaseProduct.DescrPrd
                BaseProduct.BaseProduct.NormeCE==(bool)productToUpdate.BaseProduct.NormeCE
                contexte.SaveChanges();
            }
        }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.