EF Code First创建的外键多于预期

时间:2016-02-15 06:33:11

标签: c# entity-framework ef-code-first foreign-keys

我正在开发一个应该能够首先在代码和数据库之间切换的应用程序,所以我接受了EF生成的代码(从db开始)并尝试先创建代码。最后,数据库是相同的,除了Activity表除了TypeIdSubTypeId还包含Type_IdType_Id1之外,这两个表都可以为空,我不会# 39;不明白为什么。

public class Activity
{
    public int Id { get; set; }
    public int TypeId { get; set; }
    public int? SubTypeId { get; set; }

    [ForeignKey("TypeId")]
    public virtual Type Type { get; set; }
    [ForeignKey("SubTypeId")]
    public virtual Type Type1 { get; set; }
}

public class Type
{
    public int Id { get; set; }
    [StringLength(50)]
    [Required]
    public string Name { get; set; }

    public virtual ICollection<Activity> Activities { get; set; }
    public virtual ICollection<Activity> Activities1 { get; set; }
}

1 个答案:

答案 0 :(得分:0)

以下希望对你有用

  public class Activity
    {
        public int Id { get; set; }
        [ForeignKey("Type")]
        public int TypeId { get; set; }
        [ForeignKey("Type1")]
        public int? SubTypeId { get; set; }
        public virtual Type Type { get; set; }

        public virtual Type Type1 { get; set; }
    }