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