3

I'm running into some problems departing from convention using Entity Framework 6, Code First Fluent API.

A classic example is that I have an entity called Software. I don't want the db table to be called Softwares. It should be called Software. But there are a few other departures as well.

The problem is, 2 columns are being created for a foreign key where only 1 should be. For example, in my domain, the is a 1:m relationship between SoftwareFiles and Software. (The logic being that there may be more than 1 file relevent to a piece of software e.g. Windows XP would have more than 1 ISO associated with it, due to the service packs).

The files:

public class Software
{
    public string Description { get; set; }
    public int Id { get; set; }
    public SoftwareType Type { get; set; }
    public int TypeId { get; set; }

    public virtual ICollection<SoftwareFile> SoftwareFiles { get; set; }
}

public class SoftwareFile
{
    public int Id { get; set; }
    public string FileName { get; set; }
    public FileTypes FileType { get; set; }
    public string Name { get; set; }
    public Software Software { get; set; }
    public int SoftwareId { get; set; }
}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //  Set up the SoftwareFile table
        modelBuilder.Entity<SoftwareFile>().Property(s => s.FileName).HasMaxLength(250).IsRequired().IsVariableLength();
        modelBuilder.Entity<SoftwareFile>().Property(s => s.FileType).IsRequired();
        modelBuilder.Entity<SoftwareFile>().HasRequired(s => s.Software).WithMany().HasForeignKey(s => s.SoftwareId);

        modelBuilder.Entity<Software>().ToTable("Software");
        modelBuilder.Entity<Software>().Property(s => s.Description).HasMaxLength(250).IsOptional().IsVariableLength();
        modelBuilder.Entity<Software>().HasRequired(s => s.Type).WithMany().HasForeignKey(t => t.TypeId);


        base.OnModelCreating(modelBuilder);
    }

That is creating both a SoftwareId column and a Software_Id column in the sdf database.

Does anyone know how I can depart from convention in this way?

Cheers

Dabblernl
  • 15,831
  • 18
  • 96
  • 148
onefootswill
  • 3,707
  • 6
  • 47
  • 101

1 Answers1

1

The double foreign key has no relation to the renaming of the Table.

Remove the

 modelBuilder.Entity<SoftwareFile>().HasRequired(s => s.Software).WithMany().HasForeignKey(s => s.SoftwareId);

line.

This line of code says that there is a one sided one to many relation between Software and SoftwareFile that should use the SoftwareId property as foreign key.

But you do have a SoftwareFiles property on Software, which makes EF assume that you want to define a second, double sided, one to many relation between the two entities for which you choose not to provide an explicit foreign key.

Hence EF comes to the rescue by creating a second foreign key property named Software_Id!

Dabblernl
  • 15,831
  • 18
  • 96
  • 148
  • Yep, that worked. It also cleaned up another relationship which I did not include in the question. Thanks for the assist! – onefootswill Nov 10 '13 at 09:00