EF 框架代码优先 - 未生成正确的数据库脚本

EF Framework Code First - Not Generating the Correct DB Script

我已经创建了我的上下文 类 并且在 EF.Below 中是我在从 EF 创建脚本时遇到的两个实体。

解释一下Account和Post的关系,基本上1个Account可以有0个或者多个Post。

以下是我特别关注的 3 个场景,因为 EF 没有正确生成脚本(据我所知)。

  1. 1 个帐户可以创建许多 Post 个。
  2. 1个账号可以修改很多Post个。
  3. 1 个帐户可以发布许多 Post 个。

下面是我的两个代码 类;

[Table("Account")]
public class Account : IModificationHistory
{
    [Key]
    public int ID { get; set; }

    [Required]
    [Column("firstName")] //db column name
    [MaxLength(255, ErrorMessage = "First name should be 255 characters or less")]
    [Display(Name = "First Name")] //displayed in UI
    public string FirstName { get; set; }

    [Required]
    [Column("lastName")] //db column name
    [MaxLength(255, ErrorMessage = "Last name should be 255 characters or less")]
    [Display(Name = "Last Name")] //displayed in UI
    public string LastName { get; set; }

    [Required] 
    [Column("emailAddress")] //db column name
    [MaxLength(255, ErrorMessage = "Email address should be 255 characters or less")]
    [DataType(DataType.EmailAddress)] //for UI presentation only
    [Display(Name = "Email Address")] //displayed in UI
    [EmailAddress(ErrorMessage = "Invalid email address format")]
    public string EmailAddress { get; set; }

    [Required]
    [Column("password")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Column("status")]
    public int Status { get; set; }

    [Column("role")]
    public int Role { get; set; }

    [Column("salt")]
    public string Salt { get; set; }

    [Column("dateCreated")]
    public DateTime DateCreated { get; set; }

    [Column("dateModified")]
    public DateTime DateModified { get; set; }


    public List<Post> ListPost { get; set; }

}

[Table("Post")]
public class Post : IModificationHistory
{
    [Key]
    public int ID { get; set; }

    [Required(ErrorMessage = "Title is mandatory")]
    [Column("title")]
    [MaxLength(255, ErrorMessage = "Title should be 255 characters or less")]
    public string Title { get; set; }

    [Required(ErrorMessage = "Content is mandatory")]
    [Column("content")]
    public string Content { get; set; }

    [Column("incidentID")]
    [Display(Name = "Related Incident")]
    public string IncidentID { get; set; }

    [Column("tagID")]
    [Display(Name = "Tag")]
    public string TagID { get; set; }

    [Column("dateModified")]
    public DateTime DateModified { get; set; }

    [Column("dateCreated")]
    public DateTime DateCreated { get; set; }

    [Column("datePublished")]
    public DateTime DatePublished { get; set; }

    [Column("status")]
    public int Status { get; set; }

    [Column("contactGroupID")]
    public int ContactGroupID { get; set; }

    //foreign key reference to Account table
    [Column("createdByAccountID")]
    public int CreatedByAcccountID { get; set; }

    //foreign key reference to Account table
    [Column("modifiedByAccountID")]
    public int ModifiedByAccountID { get; set; }        

    //foreign key reference to Account table
    [Column("publishedByAccountID")]
    public int PublishedByAccountID { get; set; }


    public Account ModifiedByAccount { get; set; }

    public Account CreatedByAccount { get; set; }

    public Account PublishedByAccount { get; set; }

}

当我运行添加迁移命令时,下面是它生成的脚本

CreateTable(
            "dbo.Account",
            c => new
                {
                    ID = c.Int(nullable: false, identity: true),
                    firstName = c.String(nullable: false, maxLength: 255),
                    lastName = c.String(nullable: false, maxLength: 255),
                    emailAddress = c.String(nullable: false, maxLength: 255),
                    password = c.String(nullable: false),
                    status = c.Int(nullable: false),
                    role = c.Int(nullable: false),
                    salt = c.String(),
                    dateCreated = c.DateTime(nullable: false),
                    dateModified = c.DateTime(nullable: false),
                })
            .PrimaryKey(t => t.ID);
        
        CreateTable(
            "dbo.Post",
            c => new
                {
                    ID = c.Int(nullable: false, identity: true),
                    title = c.String(nullable: false, maxLength: 255),
                    content = c.String(nullable: false),
                    incidentID = c.String(),
                    tagID = c.String(),
                    dateModified = c.DateTime(nullable: false),
                    dateCreated = c.DateTime(nullable: false),
                    datePublished = c.DateTime(nullable: false),                      
                    status = c.Int(nullable: false),
                    contactGroupID = c.Int(nullable: false),
                    modifiedByAccountID = c.Int(nullable: false),
                    createdByAccountID = c.Int(nullable: false),
                    publishedByAccountID = c.Int(nullable: false),
                    CreatedByAccount_ID = c.Int(),
                    Account_ID = c.Int(),
                })
            .PrimaryKey(t => t.ID)
            .ForeignKey("dbo.Account", t => t.CreatedByAccount_ID)
            .ForeignKey("dbo.Account", t => t.modifiedByAccountID, cascadeDelete: true)
            .ForeignKey("dbo.Account", t => t.publishedByAccountID, cascadeDelete: true)
            .ForeignKey("dbo.Account", t => t.Account_ID)
            .Index(t => t.modifiedByAccountID)
            .Index(t => t.publishedByAccountID)
            .Index(t => t.CreatedByAccount_ID)
            .Index(t => t.Account_ID);

我的问题是在脚本中,它创建了两列“createdByAccountID = c.Int(nullable: false)”列和“CreatedByAccount_ID = c.Int()”列我认为这具有相同的目的,并创建了另一个额外的列“Account_ID = c.Int()”。谁能指出我在这里做错了什么?

谢谢。

我从 EF 中发现了上述行为的问题。因为我没有配置InverseProperty

在 Post class 中,我做了以下更改;

  • 我已经删除了以下属性

     [Column("createdByAccountID")]
     public int CreatedByAccountID { get; set; }
    
     [Column("modifiedByAccountID")]
     public int ModifiedByAccountID { get; set; }
    
     [Column("publishedByAccountID")]
     public int PublishedByAccountID { get; set; }
    
  • 并修改了以下属性

     [Column("createdByAccountID")]
     public Account CreatedByAccount { get; set; }
    
     [Column("modifiedByAccountID")]
     public Account ModifiedByAccount { get; set; }
    
     [Column("publishedByAccountID")]
     public Account PublishedByAccount { get; set; }
    

并在帐户 class 中进行了以下更改;

  • 删除了以下属性

     public List<Post> ListPost { get; set; }
    
  • 并添加了以下属性

     [InverseProperty("CreatedByAccount")]
     public List<Post> ListPostCreated { get; set; }
    
     [InverseProperty("ModifiedByAccount")]
     public List<Post> ListPostModified { get; set; }
    
     [InverseProperty("PublishedByAccount")]
     public List<Post> ListPostPublished { get; set; }
    

进行上述更改后,当我执行添加迁移时,生成了正确的数据库脚本。 下面是生成的脚本;

CreateTable(
        "dbo.Post",
        c => new
            {
                ID = c.Int(nullable: false, identity: true),
                title = c.String(nullable: false, maxLength: 255),
                content = c.String(nullable: false),
                incidentID = c.String(),
                tagID = c.String(),
                dateModified = c.DateTime(nullable: false),
                dateCreated = c.DateTime(nullable: false),
                datePublished = c.DateTime(nullable: false),                      
                status = c.Int(nullable: false),
                contactGroupID = c.Int(nullable: false),
                CreatedByAccount_ID = c.Int(),
                ModifiedByAccount_ID = c.Int(),
                PublishedByAccount_ID = c.Int(),
            })
        .PrimaryKey(t => t.ID)
        .ForeignKey("dbo.Account", t => t.CreatedByAccount_ID)
        .ForeignKey("dbo.Account", t => t.ModifiedByAccount_ID)
        .ForeignKey("dbo.Account", t => t.PublishedByAccount_ID)
        .Index(t => t.CreatedByAccount_ID)
        .Index(t => t.ModifiedByAccount_ID)
        .Index(t => t.PublishedByAccount_ID);

希望这对遇到同样问题的人有所帮助。