Can't delete child entities with DbLinq

孤人 提交于 2020-01-17 03:35:08

问题


This question is related to my previous one. You can find pretty much all the code there, with the exceptions reported here. I replaced nullable fields with int fields and where the generated code put null I put -1. In this way I managed to solve an annoying exception raised by the DbLinq code. But now I can't delete child entities! In particular, I have a patient with some addresses associated. The table PatientAddresses holds all the addresses in triplets (PatientID, Address, DomicileStatus), of which the first two members consistute a primary key for the table. I wrote a Test to point out the issue:

static void TestAddresses()
    {
        Patient p1 = CreateRandomPatient();
        PatientAddress a1 = CreateRandomAddress();

        p1.Addresses.Add(a1);
        Database.Source.Patients.InsertOnSubmit(p1);
        TestUtility.SaveCloseAndReopen();

        Patient p2 = Database.Source.Patients.Single(p => p.ID == p1.ID);

        Debug.Assert(p2.Addresses.Count == 1);
        Debug.Assert(p2.Addresses[0].PatientID == a1.PatientID && p2.Addresses[0].Address.Equals(a1.Address));
        Debug.Assert(Database.Source.PatientsAddresses.Where(a => a.PatientID == a1.PatientID && a.Address == a1.Address).Count() > 0);

        p2.Addresses.RemoveAt(0);
        TestUtility.SaveCloseAndReopen();

        p2 = Database.Source.Patients.Single(p => p.ID == p1.ID);

        Debug.Assert(p2.Addresses.Count == 0); // <-- HERE
        Debug.Assert(!Database.Source.PatientsAddresses.Contains(a1));
    }

The test fails at the indicated line. Long story short, even if I delete an address, it stays in the database. I even tried to delete the corresponding PatientAddress row, but to no avail. I tried three or four variants but I cannot remove addresses from a Patient. The code used to generate those table is the following:

create table Patients (
ID integer primary key,
FirstName text not null,
LastName  text not null,
Profession text,
IsMarried integer not null default 0,
HasChildren integer not null default 0,
Birthday integer not null    
);

create table PatientsAddresses (
PatientID integer,
Address text,
DomicileStatus text,
primary key (PatientID, Address),
foreign key (PatientID) references Patients(ID)
    on delete cascade 
    on update cascade
);

I can't figure out a way to avoid this behavior, and google doesn't help.

来源:https://stackoverflow.com/questions/12583839/cant-delete-child-entities-with-dblinq

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!