到许多关系code第一多个外键的一 [英] Code First Multiple Foreign Keys for One to Many Relation

查看:83
本文介绍了到许多关系code第一多个外键的一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些问题,从约定出发使用实体框架6,$ C C首先流利的API $。

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

一个典型的例子是,我有所谓的软件实体。我不想db表被称为软件。它应该被称为软件。但也有一些其他的出发为好。

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.

现在的问题是,目前正在为外键,其中只有1应创建2列。例如,在我的领域,在为1:SoftwareFiles和软件之间米关系。 (该逻辑是,可能有多于1个文件相应和到一个软件如Windows XP的将有超过1的ISO与之关联,由于服务包)。

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).

中的文件:

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);
    }

这是创建既是 SoftwareId 列和 Software_Id 列自卫队数据库。

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?

干杯

推荐答案

双外键没有任何关系表中的重新命名。

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

删除

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

行。

这行code说,有一个的一边倒的一个软件之间有很多关系软件文件应该使用 SoftwareId 属性作为外键。

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.

但你的执行的对软件,这使得EF承担 SoftwareFiles 属性要定义第二个,双面的,一来为你选择不提供明确的外键的两个实体之间的许多关系。

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.

因此​​EF就派上用场了通过创建一个名为二外键属性 Software_Id

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

这篇关于到许多关系code第一多个外键的一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆