过去几天我一直遇到实体框架的问题,我在周末发布了一个问题,我认为这是我的问题,但事实证明,我认为这是我的模型或实体框架本身。所以,我首先尝试创建最能描述我的实体应该如何定义的模型类,然后让EF为我生成数据库。在大多数情况下,它做对了,直到我添加了另一个与另一个相关的类,并且它发出了关于级联删除的警告。感谢@Florian Haider,他指示我如何告诉EF禁用级联删除。但同样,这并不能解决我的实际问题。我相信我的模型或数据库是不正确的。
这个问题旨在描述我在VS2015中从数据库创建数据模型时遇到的问题,我认为它应该在相反的方向上创建。所以这里:
我有下表:
CREATE TABLE [dbo].[AcademicYear](
[ID] [uniqueidentifier] NOT NULL,
[Name] [nvarchar](max) NULL,
[BeginDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL,
[SchoolID] [uniqueidentifier] NOT NULL,
[CreateDate] [datetime] NOT NULL,
[UpdateDate] [datetime] NULL,
[Active] [bit] NOT NULL,
CONSTRAINT [PK_AcademicYear] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[AcademicYear] ADD CONSTRAINT [DF_AcademicYear_ID] DEFAULT (newid()) FOR [ID]
GO
ALTER TABLE [dbo].[AcademicYear] ADD CONSTRAINT [DF_AcademicYear_CreateDate] DEFAULT (getdate()) FOR [CreateDate]
GO
ALTER TABLE [dbo].[AcademicYear] WITH CHECK ADD CONSTRAINT [FK_AcademicYear_AcademicYear] FOREIGN KEY([SchoolID])
REFERENCES [dbo].[School] ([ID])
GO
ALTER TABLE [dbo].[AcademicYear] CHECK CONSTRAINT [FK_AcademicYear_AcademicYear]
GO
然后我有相关的表格:
CREATE TABLE [dbo].[School](
[ID] [uniqueidentifier] NOT NULL,
[Name] [nvarchar](256) NOT NULL,
[MissionStatement] [nvarchar](1024) NULL,
[AddressLine1] [nvarchar](1024) NOT NULL,
[AddressLine2] [nvarchar](1024) NULL,
[City] [nvarchar](256) NOT NULL,
[StateCode] [nvarchar](256) NOT NULL,
[PostalCode] [nvarchar](256) NOT NULL,
[PhoneNumber] [nvarchar](256) NOT NULL,
[FaxNumber] [nvarchar](256) NULL,
[EmailAddress] [nvarchar](256) NULL,
[WebsiteURL] [nvarchar](1024) NULL,
[CurrentAcademicYearID] [uniqueidentifier] NULL,
[EnrollmentAcademicYearID] [uniqueidentifier] NULL,
[CreateDate] [datetime] NOT NULL,
[UpdateDate] [datetime] NULL,
[Active] [bit] NOT NULL,
CONSTRAINT [PK_School] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[School] ADD CONSTRAINT [DF_School_ID] DEFAULT (newid()) FOR [ID]
GO
ALTER TABLE [dbo].[School] ADD CONSTRAINT [DF_School_CreateDate] DEFAULT (getdate()) FOR [CreateDate]
GO
ALTER TABLE [dbo].[School] WITH CHECK ADD CONSTRAINT [FK_School_AcademicYear] FOREIGN KEY([CurrentAcademicYearID])
REFERENCES [dbo].[AcademicYear] ([ID])
GO
ALTER TABLE [dbo].[School] CHECK CONSTRAINT [FK_School_AcademicYear]
GO
ALTER TABLE [dbo].[School] WITH CHECK ADD CONSTRAINT [FK_School_AcademicYear1] FOREIGN KEY([EnrollmentAcademicYearID])
REFERENCES [dbo].[AcademicYear] ([ID])
GO
ALTER TABLE [dbo].[School] CHECK CONSTRAINT [FK_School_AcademicYear1]
GO
在VS2015中创建实体数据模型后,生成的模型类如下所示......
学年:
[Table("AcademicYear")]
public partial class AcademicYear
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public AcademicYear()
{
CellGroup = new HashSet<CellGroup>();
FeeSchedule = new HashSet<FeeSchedule>();
ParentEnrollment = new HashSet<ParentEnrollment>();
ReportingPeriod = new HashSet<ReportingPeriod>();
School1 = new HashSet<School>();
School2 = new HashSet<School>();
StudentEnrollment = new HashSet<StudentEnrollment>();
VolunteerPosition = new HashSet<VolunteerPosition>();
}
public Guid ID { get; set; }
public string Name { get; set; }
public DateTime BeginDate { get; set; }
public DateTime EndDate { get; set; }
public Guid SchoolID { get; set; }
public DateTime CreateDate { get; set; }
public DateTime? UpdateDate { get; set; }
public bool Active { get; set; }
public virtual School School { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<CellGroup> CellGroup { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<FeeSchedule> FeeSchedule { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ParentEnrollment> ParentEnrollment { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ReportingPeriod> ReportingPeriod { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<School> School1 { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<School> School2 { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<StudentEnrollment> StudentEnrollment { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<VolunteerPosition> VolunteerPosition { get; set; }
}
学校:
[Table("School")]
public partial class School
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public School()
{
AcademicYear = new HashSet<AcademicYear>();
Parent = new HashSet<Parent>();
Student = new HashSet<Student>();
}
public Guid ID { get; set; }
[Required]
[StringLength(256)]
public string Name { get; set; }
[StringLength(1024)]
public string MissionStatement { get; set; }
[Required]
[StringLength(1024)]
public string AddressLine1 { get; set; }
[StringLength(1024)]
public string AddressLine2 { get; set; }
[Required]
[StringLength(256)]
public string City { get; set; }
[Required]
[StringLength(256)]
public string StateCode { get; set; }
[Required]
[StringLength(256)]
public string PostalCode { get; set; }
[Required]
[StringLength(256)]
public string PhoneNumber { get; set; }
[StringLength(256)]
public string FaxNumber { get; set; }
[StringLength(256)]
public string EmailAddress { get; set; }
[StringLength(1024)]
public string WebsiteURL { get; set; }
public Guid? CurrentAcademicYearID { get; set; }
public Guid? EnrollmentAcademicYearID { get; set; }
public DateTime CreateDate { get; set; }
public DateTime? UpdateDate { get; set; }
public bool Active { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AcademicYear> AcademicYear { get; set; }
public virtual AcademicYear AcademicYear1 { get; set; }
public virtual AcademicYear AcademicYear2 { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Parent> Parent { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Student> Student { get; set; }
}
现在,就我个人而言,我认为该模型应如下所示......
学年:
[Table("AcademicYear")]
public class AcademicYear : BaseEntity
{
public string Name { get; set; }
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}", ApplyFormatInEditMode = true)]
public DateTime BeginDate { get; set; }
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}", ApplyFormatInEditMode = true)]
public DateTime EndDate { get; set; }
public virtual ICollection<ReportingPeriod> ReportingPeriods { get; set; }
//public virtual ICollection<VolunteerPosition> VolunteerPositions { get; set; }
public virtual ICollection<CellGroup> CellGroups { get; set; }
public virtual ICollection<ParentEnrollment> ParentEnrollments { get; set; }
public virtual ICollection<StudentEnrollment> StudentEnrollments { get; set; }
}
学校:
[Table("School")]
public class School : BaseEntity
{
public string Name { get; set; }
public string MissionStatement { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string StateCode { get; set; }
public string PostalCode { get; set; }
public string PhoneNumber { get; set; }
public string FaxNumber { get; set; }
public string EmailAddress { get; set; }
public string WebsiteURL { get; set; }
public Guid? CurrentAcademicYearID { get; set; }
[ForeignKey("CurrentAcademicYearID")]
public virtual AcademicYear CurrentAcademicYear { get; set; }
public Guid? EnrollmentAcademicYearID { get; set; }
[ForeignKey("EnrollmentAcademicYearID")]
public virtual AcademicYear EnrollmentAcademicYear { get; set; }
public virtual ICollection<Parent> Parents { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
我错了吗?这是我的桌子或EF(或两者)的问题吗? School1和School2的藏品来自哪里?