tags:

views:

264

answers:

1

Hi everyone, 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.

Problem : Entity is not associate already

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 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;
        }
    }

Thanks in advance!

+3  A: 

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);

eglasius
I don't know how many hours I lost on this very problem - no error message, no exceptions, the code would execute fine, but my DB would not have updated values. My tables had auto-increment ID's but I hadn't told SQL Server that they were PK's ... silent FAIL.
Matt