Table referencing multiple tables. Error in save “Referential integrity constraint violation.”
Clash Royale CLAN TAG#URR8PPP
Table referencing multiple tables. Error in save “Referential integrity constraint violation.”
I have tables in the following structure.
Table1 Key, DocKey, Name
Table2 Key, DocKey, Name
Table3 Key, DocKey, Name
Table4 Key, DocKey, T1Key, T2Key, T3Key, Name, Value
I have defined the foreign key relation ship in the following way. What i am trying to do is, to make the T4 dependent and have navigational properties to T1, T2 and T3.
modelBuilder.Entity<Table4>()
.HasRequired<Table3>(d => d.Table3Nav)
.WithMany()
.HasForeignKey(k => new k.DocKey, k.T3Key );
modelBuilder.Entity<Table4>()
.HasRequired<Table2>(d => d.Table2Nav)
.WithMany()
.HasForeignKey(k => new k.DocKey, k.T2Key );
modelBuilder.Entity<Table4>()
.HasRequired<Table1>(d => d.Table1Nav)
.WithMany()
.HasForeignKey(k => new k.DocKey, k.T1Key );
I am able to add objects, but when i am trying to save, i am getting the following error. tried searching through similar errors, but unable to figure out what need to change. Please assist.
Save
public int SaveDoc(Doc Document)
if (Document.Key == 0)
data.Docs.Add(Document);
else
var currDoc = data.Docs.Where(x => x.Key == Document.Key).FirstOrDefault();
data.Entry(currDoc).CurrentValues.SetValues(Document);
data.SaveChanges();
return Document.Key;
Referential integrity constraint violation. A Dependent Role has
multiple principals with different values.
WillCascadeOnDelete(false)
modelBuilder.Entity<Table4>
@TetsuyaYamamoto, added that but still gives the error. I have updated the question to include the saving. is anything need to be done there? I am adding and deleting entities in the Business logic before i send the object to the save function.
– Hybridzz
Aug 6 at 2:12
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Try setting
WillCascadeOnDelete(false)
for allmodelBuilder.Entity<Table4>
definitions. Which code that attempts to add (and save) new objects?– Tetsuya Yamamoto
Aug 6 at 2:03