简体   繁体   English

EF Code First创建的外键超出预期

[英]EF Code First creates more foreign keys than expected

I'm working on an application that should be able to switch between code first and database first, so I took the code generated by EF (from db first) and tried to create code first. 我正在开发一个应该首先在代码和数据库之间进行切换的应用程序,因此我采用了EF生成的代码(首先从数据库),并尝试首先创建代码。 In the end, the databases were the same, except this Activity table that besides TypeId and SubTypeId contains also Type_Id and Type_Id1 both nullable and I don't understand why. 最后,数据库是相同的,除了这个Activity表,除了TypeIdSubTypeId之外还包含Type_IdType_Id1都是可空的,我也不知道为什么。

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

Hope below will work for you 希望下面能为您服务

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM