0

I am using EF 6 code first. I have a following situation in a database design:

I have 3 tables i.e.

  • Student
  • Person
  • Contact

Through code first Fluent API i have established a uni-directional 1 to 1 mapping between the following:

  • Student - Contact
  • Person - Contact

When i established the relationship between Student - Contact everything is perfectly working. But when i added the next relationship i.e. between Person - Contact then i got the following exception:

The operation failed because an index or statistics with name 'IX_Id' already exists on table 'dbo.Contacts'.

Now i know that because of shared primary key concept the Primary key of Contact is the FK of Student and then when i added the new relationship i.e. between Person - Contact force the EF to create the new FK for Person and because the FK with the same name already exist which cause the exception.

I want to know that is there any workaround for this situation ??

I have to keep the contact table separate because lots of other tables in my database like Student, Person, Firm, etc have there contact information stored in contact table. So i have to create a lot of other 1 to 1 relationship with contact also.

2

1 Answer 1

0

Without knowing entity framework or code first, I think the issue would be that your tables need to look like this:

Contact:  
   contact_id primary_key

Person:  
    person_id primary_key
    contact_id foreign_key

Student:  
    student_id primary_key
    contact_id foreign_key

Based on the error message it would appear you are trying to do something like this:

Contact:  
       contact_id primary_key
       person_student_id foreign_key -- either to Person or Student
    Person:  
        person_id primary_key
        contact_id foreign_key

    Student:  
        student_id primary_key
        contact_id foreign_key

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.