迁移会创建意外的ID字段,尽管未明确指定

时间:2019-01-12 19:36:51

标签: c# entity-framework code-first ef-migrations

C#代码首次迁移会创建附加字段,即PostedBy,尽管未明确指定。

我在子类(CensoredBy)中创建了一些属性,例如LessonComment[WebsiteUser].[UserID],这些属性引用了父类字段Update-database -script。但是,当我运行命令[WebsiteUser_UserID]时,它将在子类中创建一个字段public partial class WebsiteUser { public WebsiteUser() { this.Courses = new List<Course>(); this.LessonComments = new List<LessonComment>(); this.UserLessons = new List<UserAssessLesson>(); } [Key] public int UserID { get; set; } [Required] public string Name { get; set; } [Required] public string Email { get; set; } [MaxLength(200)] [Required] [ScaffoldColumn(false)] public string PasswordHash { get; set; } [ForeignKey("Role")] public Nullable<int> RoleID { get; set; } public virtual Role Role { get; set; } public virtual ICollection<Course> Courses { get; set; } // New public virtual ICollection<LessonComment> LessonComments { get; set; } public virtual ICollection<UserAssessLesson> UserLessons { get; set; } }

WebsiteUser.cs

public partial class LessonComment
{
    [Key]
    public int CommentID { get; set; }
    [MaxLength(500)]
    public string Comment { get; set; }  

    [ForeignKey("CourseLesson")]
    public int LessonID { get; set; }

    [ForeignKey("PostedBy")]
    public int PostedByUserID { get; set; }

    public DateTime DatePosted { get; set; }        

    //  CENSORSHIP DETAIL (If comment is censored)
    public Nullable<bool> IsCensored { get; set; }  // Comment will not be displayed until verified
    [ForeignKey("CensoredBy")]
    public int CensoredByUserID { get; set; }
    public Nullable<DateTime> CensorDate { get; set; }
    [MaxLength(100)]
    public string CensorReason { get; set; }  

    //  Navigation Properties
    public virtual CourseLesson CourseLesson { get; set; }
    public virtual WebsiteUser PostedBy { get; set; }
    public virtual WebsiteUser CensoredBy { get; set; }

}

LessonComment.cs

CREATE TABLE [dbo].[WebsiteUser] (
[UserID] [int] NOT NULL IDENTITY,
[Name] [nvarchar](max) NOT NULL,
[Email] [nvarchar](max) NOT NULL,
[PasswordHash] [nvarchar](200) NOT NULL,
[RoleID] [int],
CONSTRAINT [PK_dbo.WebsiteUser] PRIMARY KEY ([UserID])
)

CREATE TABLE [dbo].[LessonComment] (
[CommentID] [int] NOT NULL IDENTITY,
[Comment] [nvarchar](500),
[LessonID] [int] NOT NULL,
[PostedByUserID] [int] NOT NULL,
[DatePosted] [datetime] NOT NULL,
[Censored] [bit],
[CensoredByUserID] [int] NOT NULL,
[CensorDate] [datetime],
[CensorReason] [nvarchar](100),
[WebsiteUser_UserID] [int],  -- How is this created ???
CONSTRAINT [PK_dbo.LessonComment] PRIMARY KEY ([CommentID])
)
CREATE INDEX [IX_LessonID] ON [dbo].[LessonComment]([LessonID])
CREATE INDEX [IX_PostedByUserID] ON [dbo].[LessonComment]([PostedByUserID])
CREATE INDEX [IX_CensoredByUserID] ON [dbo].[LessonComment]([CensoredByUserID])
CREATE INDEX [IX_WebsiteUser_UserID] ON [dbo].[LessonComment]([WebsiteUser_UserID])    
ALTER TABLE [dbo].[LessonComment] ADD CONSTRAINT [FK_dbo.LessonComment_dbo.WebsiteUser_CensoredByUserID] FOREIGN KEY ([CensoredByUserID]) REFERENCES [dbo].[WebsiteUser] ([UserID]) ON DELETE CASCADE
ALTER TABLE [dbo].[LessonComment] ADD CONSTRAINT [FK_dbo.LessonComment_dbo.CourseLesson_LessonID] FOREIGN KEY ([LessonID]) REFERENCES [dbo].[CourseLesson] ([LessonID]) ON DELETE CASCADE
ALTER TABLE [dbo].[LessonComment] ADD CONSTRAINT [FK_dbo.LessonComment_dbo.WebsiteUser_PostedByUserID] FOREIGN KEY ([PostedByUserID]) REFERENCES [dbo].[WebsiteUser] ([UserID]) ON DELETE CASCADE
ALTER TABLE [dbo].[LessonComment] ADD CONSTRAINT [FK_dbo.LessonComment_dbo.WebsiteUser_WebsiteUser_UserID] FOREIGN KEY ([WebsiteUser_UserID]) REFERENCES [dbo].[WebsiteUser] ([UserID])  --No idea how this is created

SQL脚本

{{1}}

预期结果是不应创建附加字段“ [WebsiteUser_UserID] [int]”。

1 个答案:

答案 0 :(得分:0)

使用父类中的 InverseProperty 注释解决:

public partial class WebsiteUser
{
// ...
    [InverseProperty("PostedBy")] // Solved the problem
    public virtual ICollection<LessonComment> LessonComments { get; set; }
}

注意::由于子类中有1个以上的外键( PostedBy,CensoredBy ),因此需要明确提供InverseProperty父类中的注释。

阅读以下[article](http://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in-code-first.aspx“ InverseProperty-Data Annotations-attribute-in-code-first”)