1

I have an error here and can't quite get the logic behind it. I am using Entity Framework 6 with a model-first approach. I have designed two tables that reference each other like this:

enter image description here

Basically, an account needs to be in a company while a company has to have an account as an administrator. However, I get an error from the EF validator:

Error 3014: Problem in mapping fragments starting at lines 224, 249:The foreign key 'Foreign key constraint 'CompaniesAccounts1' from table CompaniesSet (Id) to table AccountsSet (Id):' is not being enforced in the model. An Association or inheritance relationship needs to be created to enforce this constraint.

CompaniesAccounts1 is the first association (1...*) you see here, Accounts being the principal. The second association has Companies as principal. Anybody know why I am getting this error?

All 4 properties are non-nullable Int32 types.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
LeonidasFett
  • 3,052
  • 4
  • 46
  • 76
  • 2
    From a relational standpoint, what you are trying to do here is not possible. At least one of the properties has to be nullable. You are creating a chicken and egg problem here. You need a company record to attach the admin account to, but you need an account record for that account to assign as the admin account on the company record. –  Jan 21 '16 at 21:44
  • Ok I changed CompanyId to nullable and the second relation to * and 0..1, respectively. I still get the error. Am I still missing something? – LeonidasFett Jan 21 '16 at 21:51
  • Hmmm, weird. I inspected the designer xml file and found a few artifacts from associations that were no longer there. After deleting those, the sql file and the Model.Context.tt and Model.tt, it sorted itself out. I now got 0..1 and * on one association and * and 0...1 on the other. – LeonidasFett Jan 21 '16 at 22:23

1 Answers1

0

Due to AgapwIesu's comment, I came up with the following solution:

enter image description here

Strange thing is, and I noticed because I already tried it like this and still got the error above. After digging through the xml files of the entity model, I found a few old references that I didn't use anymore. So they messed with my existing model. After deleting those and the generated files/database script and rebuilding everything, it worked.

LeonidasFett
  • 3,052
  • 4
  • 46
  • 76