none
they are not in the same type hierarchy or do not have a valid one to one foreign key relationship with matching primary keys between them. RRS feed

  • Question

  • Hello ,

    •   I have a following Data base Diagram .
    • 
    •  In the entity frame work I have created the following entities :

                public class User
                  {

                     // Members

                 }

           public class Doctor : User
            {

                 // Doctor Members

                 public virtual DoctorPhoto Photo { get; set; }

            }

            public class DoctorPhoto
            {
                public int Id { get; set; }
               public byte[] Photo { get; set; }
               public byte[] Thumbnail { get; set; }
           }

    •  Entity Type Configuration :

          public class UserEntityTypeConfigurations : EntityTypeConfiguration<User>
           {
                public UserEntityTypeConfigurations()
                {
                   this.ToTable("UmUser");
               }
        }

    public class DoctorEntityTypeConfiguration : EntityTypeConfiguration<Doctor>
        {
            public DoctorEntityTypeConfiguration()
            {
                this.ToTable("Doctors");
                Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            }
        }

    public class DoctorPhotoEntityTypeConfiguration : EntityTypeConfiguration<DoctorPhoto>
        {
            public DoctorPhotoEntityTypeConfiguration()
            {
                this.ToTable("Doctors");
            }
        }

    • When I run the application I got the following exception :

             The entity types 'DoctorPhoto' and 'User' cannot share table 'Doctors' because they are not in the same type hierarchy or           do not have a valid one to one foreign key relationship with matching primary keys between them.

        How Can I solve this problem ?


    ASk

    Monday, December 28, 2015 11:21 AM

Answers

  • Hi himo,

    According to your description, it seems a EF Designer TPT Inheritance pattern. The following is a link about TPT Inheritance pattern for your reference.

    https://msdn.microsoft.com/en-us/data/jj618293

    According to your code snippet, DoctorPhoto is not a derived class. I recommend you modify some class as below.


    public class Doctor : User { // Doctor Members public virtual int PhotoId {get;set;} public virtual DoctorPhoto Photo { get; set; } } public class DoctorEntityTypeConfiguration : EntityTypeConfiguration<Doctor> { public DoctorEntityTypeConfiguration() { this.ToTable("Doctors"); Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); HasOptional(x => x.Photo) .WithMany() .HasForeignKey(x => x.PhotoId) } } public class DoctorPhotoEntityTypeConfiguration : EntityTypeConfiguration<DoctorPhoto> { public DoctorPhotoEntityTypeConfiguration()

    {

    this.ToTable("DoctorPhoto");

    // this.ToTable("Doctors"); this may be a spell error } }

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Tuesday, December 29, 2015 9:44 AM
    Moderator

All replies

  • Hello ,

    •   I have a following Data base Diagram .
    • 
    •  In the entity frame work I have created the following entities :

                public class User
                  {

                     // Members

                 }

           public class Doctor : User
            {

                 // Doctor Members

                 public virtual DoctorPhoto Photo { get; set; }

            }

            public class DoctorPhoto
            {
                public int Id { get; set; }
               public byte[] Photo { get; set; }
               public byte[] Thumbnail { get; set; }
           }

    •  Entity Type Configuration :

          public class UserEntityTypeConfigurations : EntityTypeConfiguration<User>
           {
                public UserEntityTypeConfigurations()
                {
                   this.ToTable("UmUser");
               }
        }

    public class DoctorEntityTypeConfiguration : EntityTypeConfiguration<Doctor>
        {
            public DoctorEntityTypeConfiguration()
            {
                this.ToTable("Doctors");
                Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            }
        }

    public class DoctorPhotoEntityTypeConfiguration : EntityTypeConfiguration<DoctorPhoto>
        {
            public DoctorPhotoEntityTypeConfiguration()
            {
                this.ToTable("Doctors");
            }
        }

    • When I run the application I got the following exception :

             The entity types 'DoctorPhoto' and 'User' cannot share table 'Doctors' because they are not in the same type hierarchy or           do not have a valid one to one foreign key relationship with matching primary keys between them.

        How Can I solve this problem ?


    ASk

    Doctor

    DoctorID - primary key

    UMUser

    UserID   - primary key

    DoctorID - foreign key relationship to Doctor DoctorID  primary key

    http://www.w3schools.com/sql/sql_foreignkey.asp

    Monday, December 28, 2015 12:36 PM
  • Hi himo,

    According to your description, it seems a EF Designer TPT Inheritance pattern. The following is a link about TPT Inheritance pattern for your reference.

    https://msdn.microsoft.com/en-us/data/jj618293

    According to your code snippet, DoctorPhoto is not a derived class. I recommend you modify some class as below.


    public class Doctor : User { // Doctor Members public virtual int PhotoId {get;set;} public virtual DoctorPhoto Photo { get; set; } } public class DoctorEntityTypeConfiguration : EntityTypeConfiguration<Doctor> { public DoctorEntityTypeConfiguration() { this.ToTable("Doctors"); Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); HasOptional(x => x.Photo) .WithMany() .HasForeignKey(x => x.PhotoId) } } public class DoctorPhotoEntityTypeConfiguration : EntityTypeConfiguration<DoctorPhoto> { public DoctorPhotoEntityTypeConfiguration()

    {

    this.ToTable("DoctorPhoto");

    // this.ToTable("Doctors"); this may be a spell error } }

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Tuesday, December 29, 2015 9:44 AM
    Moderator