3

I am writing an application which uses inheritance and I'm trying to map this to a SQL Server database with TPT structure.

However, for some reason EF generates duplicate foreign keys in both the superclass and subclass tables.

I have these classes:

public abstract class Answer
{
    public int AnswerId { get; set; }
    [Required]
    [MaxLength(250, ErrorMessage = "The answer cannot contain more than 250 characters")]
    public String Text { get; set; }
    [Required]
    [MaxLength(1500, ErrorMessage = "The description cannot contain more than 1500 characters")]
    public String Description { get; set; }

    public User User { get; set; }
}

public class AgendaAnswer : Answer
{
    [Required]
    public AgendaModule AgendaModule { get; set; }
}

public class SolutionAnswer : Answer
{
    [Required]
    public SolutionModule SolutionModule { get; set; }
}

public abstract class Module
{
    // for some reason EF doesn't recognize this as primary key
    public int ModuleId { get; set; }
    [Required]
    public String Question { get; set; }
    public String Description { get; set; }
    [Required]
    public DateTime StartTime { get; set; }
    [Required]
    public DateTime EndTime { get; set; }

    public virtual IList<Tag> Tags { get; set; }
}

public class AgendaModule : Module
{
    public IList<AgendaAnswer> AgendaAnswers { get; set; }
}

public class SolutionModule : Module
{
    public IList<SolutionAnswer> SolutionAnswers { get; set; }
}

public class User
{
    public int UserId { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public int Zip { get; set; }
    public bool Active { get; set; }

    public virtual IList<AgendaAnswer> AgendaAnswers { get; set; }
    public virtual IList<SolutionAnswer> SolutionAnswers { get; set; }
}

And this is the content of my DbContext class:

public DbSet<AgendaModule> AgendaModules { get; set; }
public DbSet<SolutionModule> SolutionModules { get; set; }
public DbSet<AgendaAnswer> AgendaAnswers { get; set; }
public DbSet<SolutionAnswer> SolutionAnswers { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

    modelBuilder.Entity<Module>().HasKey(m => m.ModuleId);
    modelBuilder.Entity<Answer>().HasKey(a => a.AnswerId);

    modelBuilder.Entity<AgendaAnswer>().Map(m =>
    {
        m.ToTable("AgendaAnswers");
    });

    modelBuilder.Entity<SolutionAnswer>().Map(m =>
    {
        m.ToTable("SolutionAnswers");
    });
}

When I run my application, EF creates the tables how I want them (TPT), but it duplicates the foreign key to users in each of them (see picture).

enter image description here

Thanks in advance

OJ Raqueño
  • 4,471
  • 2
  • 17
  • 30
Sn0wBlind
  • 35
  • 4

1 Answers1

1

As noted in my comment above, the solution is to remove the AgendaAnswers and SolutionAnswers properties from the User class.

If you want to keep those collections in the User class, you might have to remove the User and UserId properties from the Answer class and instead duplicate them in the AgendaAnswer and SolutionAnswer classes. See this SO question for more information.

Community
  • 1
  • 1
OJ Raqueño
  • 4,471
  • 2
  • 17
  • 30