简体   繁体   中英

More than one record is updated EF-Linq C#

I have the following table:

在此处输入图片说明

I have several arrays, where I will scroll if the data match to decrease its amount:

//DISMINUYE CANTIDAD DE CASILLER
        public void disminuyeCasiller(string[] codParte, string [] rolls, double[] cantResta)
        {
            int size = codParte.Length;

            for(int i = 0; i < size; i++)
            {
                string parte = codParte[i];
                string rol = rolls[i];
                double valorRes = cantResta[i];

                using(var ctx=new ModelContext())
                {
                    Casiller updateRollo = ctx.Casillers.Where(x => x.cod_parte == parte && x.rollo == rol).First();
                    double newValue = updateRollo.cantidad - valorRes;
                    updateRollo.cantidad = newValue;
                    ctx.SaveChanges();
                }
            }
        }

According to what this code does, it is first to know what size the arrangements are (all the arrays will have the same size), create a for and get the amount if the rollo and the cod_parte match, to that amount you recover in this case 300 will be subtract what arrives in cantResta for example 100.50 , once subtraction is assigned in the place it was, save the changes and repeat if necessary, until there everything is fine.

I am passing this data:

codparte[]=new array{"11155"};
rollos[]=new array{"RT0102"};
cantRest[]=new array{100.50};

//Size and data can be different -only a example 

at the end of cantidad with rollo RT0102 in the table I should stay like this: 199.50 , the problem is that update both records and it looks like this:

在此处输入图片说明

Why also update RT0103 when this row is not being selected? What am I doing wrong in the sentence? (can be a problem in the future cuz many rollos has the same information)

SCREENSHOT BREAKPOINT

在此处输入图片说明

Only cod_parte is a primary key

This is the problem.

A primary key must be unique, but in your case you have more than one record with the same cod_parte . So it's not even a key, let alone primary.

When you call SaveChanges() , the EF will generate a SQL statement with a WHERE clause that contains the primary key and nothing else, assuming that the primary key will identify exactly one record. That assumption is false, because your "primary key" isn't really a key.

For this table you need a compound primary key of at least cod_parte and rollo , or, as Robert has noted, a genuine synthetic primary key.

See also How to specify a compound key in EF .

Please check your passing data , if only pass codParte=111555,rolls=RT0102,cantRest=100.50 then it won't update RT0103 .

ex:

public class Test{
    public static void Main(){
        disminuyeCasiller(new[] { "111555"}, new[] { "RT0102"}, new[] {100.50});
    }

    public static void disminuyeCasiller(string[] codParte, string[] rolls, double[] cantResta)
    {
        int size = codParte.Length;

        for (int i = 0; i < size; i++)
        {
            string parte = codParte[i];
            string rol = rolls[i];
            double valorRes = cantResta[i];
            Console.WriteLine($"parte {parte}/rol {rol}/valorRes {valorRes}"); //Result:parte 111555/rol RT0102/valorRes 100.5
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM