Код C # При первой миграции создается дополнительное поле, а именно [WebsiteUser_UserID] [int]
, хотя это не указано явно.
Я создал несколько свойств, таких как PostedBy
и CensoredBy
в дочернем классе (LessonComment
), который ссылается на поле родительского класса [WebsiteUser].[UserID]
. Однако когда я запускаю команду Update-database -script
, она создает поле [WebsiteUser_UserID]
в дочернем классе.
WebsiteUser.cs
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; }
}
LessonComment.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; }
}
SQL Script
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
Ожидаемый результат: дополнительное поле `[WebsiteUser_UserID] [int] 'не должно создаваться.