0

I have a contact table

public class Contact : EntityBase
{
    public int TrivialContactProperty { get; set; }
    ...
    public virtual FiContact FiContact { get; set; }
    public virtual PuContact PuContact { get; set; }
    public virtual TrContact TrContact { get; set; }
}

and then a FiContact, PuContact, and TrContact table.

public class FiContact : EntityBase
{
    public int TrivialFiProperty { get; set; }
    ...
    public virtual Contact Contact { get; set; }
}

public class PuContact : EntityBase
{
    public int TrivialPuProperty { get; set; }
    ...
    public virtual Contact Contact { get; set; }
}

public class TrContact : EntityBase
{
    public int TrivialTRProperty { get; set; }
    ...
    public virtual Contact Contact { get; set; }
}

The contact table should have a zero or one relationship with all three other tables. So a contact can exist without any of the other three, or it can be related to one or two or all three of them.

Using fluent API I tried to configure this, after doing some research, and I came up with:

modelBuilder.Entity<FiContact>()
    .HasRequired(r => r.Contact)
    .WithOptional(o => o.FiContact);

modelBuilder.Entity<PuContact>()
    .HasRequired(r => r.Contact)
    .WithOptional(o => o.PuContact);

modelBuilder.Entity<TrContact>()
    .HasRequired(r => r.Contact)
    .WithOptional(o => o.TrContact);

But I am still getting the following error when I try to add a migration for this change:

FiContact_Contact_Source: : Multiplicity is not valid in Role 'FiContact_Contact_Source' in relationship 'FiContact_Contact'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '(asterisk symbol here)'.

PuContact_Contact_Source: : Multiplicity is not valid in Role 'PuContact_Contact_Source' in relationship 'PuContact_Contact'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '(asterisk symbol here)'.

TrContact_Contact_Source: : Multiplicity is not valid in Role 'TrContact_Contact_Source' in relationship 'TrContact_Contact'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '(asterisk symbol here)'.

From more research I saw that the primary key on the dependent entity is supposed to also be the foreign key? The only problem is that all of my entities inherit from a class "EntityBase" which defines common fields in all entities, including the primary key:

public abstract class EntityBase : IEntity
{
    [Key]
    public int Id { get; set;}
    public bool IsActive { get; set; }
    public DateTime CreatedOnDate { get; set; }

    public int? CreatedByUserId { get; set; }

    [ForeignKey("CreatedByUserId")]
    public User CreatedByUser { get; set; }

    public DateTime LastUpdatedOnDate { get; set; }
    public int? LastUpdatedByUserId { get; set; }

    [ForeignKey("LastUpdatedByUserId")]
    public User LastUpdatedByUser { get; set; }
}

Is there a way to make this kind of table/entity relationship work with EF 6 Code First? Any help getting this type of relationship to work would be much appreciated.

Community
  • 1
  • 1
  • Have you mapped your primary keys? – Kinetic Dec 30 '16 at 04:56
  • You should post your context. – Kinetic Dec 30 '16 at 05:20
  • Have you tried to remove `public int Id { get; set; }` in derived classes? So from `Contact`, `FiContact` etc. – erikscandola Dec 30 '16 at 07:34
  • @Kinetic My primary keys are mapped in the derived EntityBase class (public int Id {get; set;}), that class is posted above. – Isaiah Justman Dec 30 '16 at 18:25
  • @Kinetic I don't think my context class will add any information. I posted the fluent configurations related to this. Other than that, my context is just defining my DbSet<> entities, and there are fluent configurations for two other unrelated entities in the OnModelCreating() Method. – Isaiah Justman Dec 30 '16 at 18:27
  • @erikscandola I can't remove that from the derived class, it affects every entity in my db. Could you elaborate on how that might help? I'm willing to make the change, it will just take a bit of time to add those properties to each entity and get rid of the derived class. – Isaiah Justman Dec 30 '16 at 18:31
  • @IsaiahJustman Your entities are the derived classes(Contact, FiContact, etc). It's just that in your example, every one of your entities has a "Id" property and your base class has also that same "Id" property. – Kinetic Dec 31 '16 at 05:17
  • @Kinetic Wow, I completely missed that. I'm updating the post now. – Isaiah Justman Jan 02 '17 at 08:44

0 Answers0