Code First Multiple Foreign Keys for One to Many Relation

丶灬走出姿态 提交于 2020-01-04 02:49:06

问题


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


回答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!



来源:https://stackoverflow.com/questions/19885290/code-first-multiple-foreign-keys-for-one-to-many-relation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!