I am using Entity Framework 4 to insert values into the table in Postgres. Went I do a Context.SaveChanges I get the following error message from the dotConnect for Postgres Provider.
Referential integrity constraint violation. A Dependent Role has multiple principals with different values.
I have a Dependent/Child Table that has two Foreign Keys that reference the same Primarily Key in the Referenced/Parent Table. The Parent Table has Parent/Child Entities. FK1 reference the Parent Entity and FK2 reference the Child Entity, so the value for both FK1 and FK2 will be different values. Why can't they have different values?
I am using the New build of dotConnect for PostgreSQL 4.95.152
A Dependent Role has multiple principals with different valu
I have made a simple test and everything was inserted correctly. I had two foreign keys pointing from a detail table to master table, like this:
And the following code succeeded:
Could you please make some changes to this code so that it would reproduce the issue?
A test project illustrating the problem is an option as well.
Code: Select all
CREATE TABLE multimaster
(
id integer NOT NULL,
"value" text,
CONSTRAINT multimaster_pkey PRIMARY KEY (id)
);
CREATE TABLE multidetail
(
id integer NOT NULL,
parentid integer,
childid integer,
"value" text,
CONSTRAINT multidetail_pkey PRIMARY KEY (id),
CONSTRAINT multidetail_childid_fkey FOREIGN KEY (childid)
REFERENCES multimaster (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT multidetail_parentid_fkey FOREIGN KEY (parentid)
REFERENCES multimaster (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
Code: Select all
using (DataModel1Entities db = new DataModel1Entities()) {
Multimaster mm = new Multimaster
{
Id = 3,
Value = "inserted"
};
var masters = db.Multimasters.ToList();
Multidetail md1 = new Multidetail
{
Id = 3,
Multimaster = masters.Where(m => m.Id == 1).SingleOrDefault(),
Multimaster1 = mm,
Value = "inserted"
};
Multidetail md2 = new Multidetail
{
Id = 4,
Multimaster = mm,
Multimaster1 = masters.Where(m => m.Id == 2).SingleOrDefault(),
Value = "inserted"
};
db.AddToMultimasters(mm);
db.SaveChanges();
}
}
A test project illustrating the problem is an option as well.
This is the code but everything is working fine now. Thanks for your help with this.
using (DataModel1Entities db = new DataModel1Entities())
{
Multimaster mmparent = new Multimaster
{
Id = 3,
Value = "inserted"
};
Multimaster mmchild1 = new Multimaster
{
Id = 4,
Value = "inserted"
};
Multimaster mmchild2 = new Multimaster
{
Id = 5,
Value = "inserted"
};
Multidetail md1 = new Multidetail
{
Id = 3,
Multimaster = mmchild1,
Multimaster1 = mmparent,
Value = "inserted"
};
Multidetail md2 = new Multidetail
{
Id = 4,
Multimaster = mmchild2,
Multimaster1 = mmparent,
Value = "inserted"
};
db.AddToMultimasters(mmparent);
db.SaveChanges();
}
}
using (DataModel1Entities db = new DataModel1Entities())
{
Multimaster mmparent = new Multimaster
{
Id = 3,
Value = "inserted"
};
Multimaster mmchild1 = new Multimaster
{
Id = 4,
Value = "inserted"
};
Multimaster mmchild2 = new Multimaster
{
Id = 5,
Value = "inserted"
};
Multidetail md1 = new Multidetail
{
Id = 3,
Multimaster = mmchild1,
Multimaster1 = mmparent,
Value = "inserted"
};
Multidetail md2 = new Multidetail
{
Id = 4,
Multimaster = mmchild2,
Multimaster1 = mmparent,
Value = "inserted"
};
db.AddToMultimasters(mmparent);
db.SaveChanges();
}
}