简体   繁体   English

多个记录已更新EF-Linq C#

[英]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. 根据这个代码做什么,这是第一个知道的安排是什么尺寸(所有的阵列将具有相同的大小),创建for并得到量如果rollocod_parte比赛,这一数额你在这个恢复情况300将减去到达cantResta (例如100.50 ,一旦在该位置分配了减法,则保存更改并在必要时重复,直到一切都好为止。

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: 在结束cantidadrollo RT0102在表中我应该留下这样的: 199.50 ,问题是,同时更新记录,它看起来是这样的:

在此处输入图片说明

Why also update RT0103 when this row is not being selected? 如果未选择此行,为什么还要更新RT0103 What am I doing wrong in the sentence? 我这句话做错了什么? (can be a problem in the future cuz many rollos has the same information) (将来可能会出现问题,因为许多rollos具有相同的信息)

SCREENSHOT BREAKPOINT 屏幕断点

在此处输入图片说明

Only cod_parte is a primary key 只有cod_parte是主键

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 . 一个主键必须是唯一的,但是在您的情况下,您有多个具有相同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. 当您调用SaveChanges() ,EF将生成一条带有WHERE子句的SQL语句,该子句包含主键,并且不包含其他任何内容,并假定主键将完全标识一条记录。 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. 对于这个表,你至少需要的复合主键cod_parterollo ,或者像罗伯特指出,一个真正的合成主键。

See also How to specify a compound key in EF . 另请参阅如何在EF中指定复合键

Please check your passing data , if only pass codParte=111555,rolls=RT0102,cantRest=100.50 then it won't update RT0103 . 请检查您的传递数据,如果仅传递codParte=111555,rolls=RT0102,cantRest=100.50 ,则不会更新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
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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