LINQ问题 [英] Problems with LINQ

查看:87
本文介绍了LINQ问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在LINQ2SQL上遇到了一些问题,首先是因为我无法使用CRUD方法,我不知道LINQ ORM有什么问题,我无法更新或删除某些已经映射到.dbml中的实体,因此我总是使用drop来解决此问题.并更新.dbml,但现在仍然无法正常工作.

I recently have some issues with LINQ2SQL, first of these as that I cant use CRUD methods I dont know whats the matter with LINQ ORM, I cant Update or Delete some entity already mapped in .dbml so I always fix this with drop and renew the .dbml , but now stills not working.

问题:实体尚未关联

System.InvalidOperationException原为 抓到Message ="No se puede quitar 整个实体都没有."
Source ="System.Data.Linq"
堆栈跟踪: zh-CN System.Data.Linq.Table`1.DeleteOnSubmit(TEntity 实体) zh-CN Resocentro.Informes.PlantillasMedicas.FormPlantilla.elimina(Int32 nro)zh D:\ cs_PlantillasMedicas \ Resocentro.Informes.PlantillasMedicas \ Resocentro.Informes.PlantillasMedicas \ FormPlantilla.xaml.cs:lnea 54 InnerException:

System.InvalidOperationException was caught Message="No se puede quitar una entidad que no se ha asociado."
Source="System.Data.Linq"
StackTrace: en System.Data.Linq.Table`1.DeleteOnSubmit(TEntity entity) en Resocentro.Informes.PlantillasMedicas.FormPlantilla.elimina(Int32 nro) en D:\cs_PlantillasMedicas\Resocentro.Informes.PlantillasMedicas\ Resocentro.Informes.PlantillasMedicas\FormPlantilla.xaml.cs:línea 54 InnerException:

PD:LINQ XML PrimaryKey属性

PD: LINQ XML PrimaryKey Attribute

用于检索实体的代码段:

Snippet for retrieving Entity :

IsPrimaryKey="true"

    private PLANTILLA getTemplateEntityFromModel(int codigoPlantilla)
    {
        using (DB db = new DB())
        {
            PLANTILLA var = db.PLANTILLAs.FirstOrDefault(x => x.codigoplantilla == codigoPlantilla);
            return var;
        }
    }

提前谢谢!

推荐答案

请检查是否为表定义了主键.如果不这样做,则可以将它们添加到表中,或者如果不能将它们直接添加到设计器中.没有主键,Linq2sql不能很好地工作,特别是在可以静默忽略的更新上.

Please check that you have primary key defined for the tables. If you don't, either add them to the tables, or if you can't to the designer directly. Linq2sql doesn't works well without primary keys, specially on updates which can be ignored silently.

此外,您是否正在检索要删除的实体,例如:

Also, are you retrieving the entity you are trying to delete, like:

var myEntity = myContext.SomeEntities.Single(e => e.MyId == 1); myContext.SomeEntities.DeleteOnSubmit(myEntity);

var myEntity = myContext.SomeEntities.Single(e=>e.MyId == 1); myContext.SomeEntities.DeleteOnSubmit(myEntity);

这篇关于LINQ问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆