我首先使用实体框架 5.0 代码。我的模型类如下所示:
public class Client
{
public Client()
{
}
public int ClientID { get; private set; }
[Required]
public string CustomerNumber { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public MeritalStatus MeritalStatus { get; set; }
public DateTime? BirthDate { get; set; }
public Gender Gender { get; set; }
public string Email { get; set; }
public string Salutation { get; set; }
public string GetFullName()
{
return this.FirstName + " " + this.LastName;
}
}
我在 global.asax 中使用 DropCreateDatabase 初始化程序:
Database.SetInitializer<SiteObjectContext>(new DropCreateDatabaseIfModelChanges<SiteObjectContext>());
因此,每次模型更改时都会重新创建 DB。
生成的数据库表如下所示:
CREATE TABLE [dbo].[Clients](
[ClientID] [int] IDENTITY(1,1) NOT NULL,
[CustomerNumber] [nvarchar](max) NOT NULL,
[FirstName] [nvarchar](max) NOT NULL,
[LastName] [nvarchar](max) NOT NULL,
[MeritalStatus] [int] NOT NULL,
[BirthDate] [datetime] NULL,
[Gender] [int] NOT NULL,
[Email] [nvarchar](max) NULL,
[Salutation] [nvarchar](max) NULL,
[Visa_VisaID] [int] NULL)
它以某种方式产生模型上不存在的冗余列 Visa_VisaID。我怀疑它可能是一种缓存,因为我之前可能在桌子上放过这一列。尝试手动删除数据库,使用 EF 6 RC 而不是 5.0,没有任何帮助,该列在每个数据库重新出现时重新出现。
签证实体:
public class Visa
{
public Visa()
{
}
public int VisaID { get; set; }
[Required]
public int ApplicationId { get; private set; }
[ForeignKey("ApplicationId")]
public virtual VisaApplication Application { get; set; }
public int MainApplicantId { get; private set; }
[ForeignKey("MainApplicantId")]
public virtual Client MainApplicant { get; set; }
public virtual ICollection<Client> IncludedCustomers { get; set; }
public int VisaTypeId { get; private set; }
[ForeignKey("VisaTypeId")]
public virtual VisaType VisaType { get; set; }
public DateTime StartDate { get; set; }
[Required]
public bool IsMultiple { get; set; }
[Required]
public DateTime ExpiryDateStay { get; set; }
public DateTime ExpiryDateTravel { get; set; }
[Required]
public DateTime FirstEntryBy { get; set; }
[Required]
public string Number { get; set; }
}
}
任何帮助表示赞赏。