开发者

Insert multiple records issue (linq EF)

开发者 https://www.devze.com 2023-04-13 08:09 出处:网络
I need to insert multiple records to different tables, the problem is that some tables have 2 different foreing keys and the EF throws me an exception. Part of my schema is shown below.

I need to insert multiple records to different tables, the problem is that some tables have 2 different foreing keys and the EF throws me an exception. Part of my schema is shown below.

Insert multiple records issue (linq EF)

And this is my code

  Credito cred = new Credito()
            {
                Producto = credito.producto,
                Tipo = credito.tipo,
                Status = credito.status,
                Cantidad = credito.monto_prestamo,
                TasaInteres = credito.tasa_interes,
                Plazo = credito.plazo,
                Periodo = credito.periodo,
                FechaInicio = credito.fecha_inicio
            };
            Cuentas cuenta = new Cuentas()
            {
                IDCredito = credito.idCredito,
                IDBanco = credito.idBanco
            };
            Grupo gpo = new Grupo()
            {
                Nombre = credito.grupo,
                IDRepre = credito.idRepGpo
            };

            context.creditos.AddObject(cred);
            context.bancos_credito.AddObject(cuenta);
            for (int i = 0; i < credito.total_plazo; i++)
                context.amortizaciones.AddObject(AgregaAmortizacion(ref fechaPago, i, credito));
            context.grupos.AddObject(gpo);
            for (int i = 0; i < renglones; i++)
            {
                context.acreditados.AddObject(AgregaAcreditado(i, credito));
                context.agrupaciones.AddObject(AgregaAgrupacion(i, credito));
            }
            context.SaveChanges();

Everything but the "context.agrupaciones.AddObject(AgregaAgrupacion(i, credito))" is fine. The issue here is that "agrupaciones" has 2 FK (id_acreditado, id_开发者_开发问答grupo) even when I'm adding "grupos" and "acreditados" objects to context. Do you know what is happening? Does the entity framework is not capable of inserting FK from two tables withouth specifying the values? Hope someone can help me, thanks


It looks like the issue is you are only setting the FK id (IDRepre, IDBanco). Usually when setting references in EF you use the entire object (cuenta.Credito = cred;).

Credito cred = new Credito() 
        { 
            Producto = credito.producto, 
            Tipo = credito.tipo, 
            Status = credito.status, 
            Cantidad = credito.monto_prestamo, 
            TasaInteres = credito.tasa_interes, 
            Plazo = credito.plazo, 
            Periodo = credito.periodo, 
            FechaInicio = credito.fecha_inicio 
        };
Cuentas cuenta = new Cuentas()   
        {   
            IDCredito = credito.idCredito,   
            IDBanco = credito.idBanco   
        }; 
cred.Cuentas.Add(cuenta);
... (more mapping code here)
context.credito.AddObject(cred); 
context.SaveChanges();

You should only need to do "AddObject" for the parent object if the children are set correctly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号