我正在尝试使用带有数据注释的 Code First 创建带有 Entity Framework 4.3 的计费数据库,每次尝试创建数据库时都会出错。以下是我正在处理的对象:
public class ClientBase
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ClientID { get; set; }
[Required]
public string ClientName { get; set; }
[Required]
public bool IsActive { get; set; }
[Required]
public string ClientContactName { get; set; }
[Required]
public string ClientContactEmail { get; set; }
public virtual List<PropertyBase> Communities { get; set; }
}
public class PropertyBase
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PropertyID { get; set; }
[ForeignKey("Client")]
public int ClientID { get; set; }
[Required, EnumDataType(typeof(BillingFrequency))]
public BillingFrequency PropertyBillingFrequency { get; set; }
[Required]
public bool IsActive { get; set; }
[Required]
public string PropertyName { get; set; }
public string PropertyStreet { get; set; }
public string PropertyCity { get; set; }
public string PropertyState { get; set; }
public int PropertyZipCode { get; set; }
public virtual ClientBase Client { get; set; }
}
无论我尝试做什么,我总是会收到此错误:
The operation failed because an index or statistics with name 'IX_ClientID' already exists on table 'Property'.
我已经使用 Fluent API 看到了这个问题的解决方案,但我还没有找到一个用于数据注释的解决方案。
有谁知道如何解决这个问题?
编辑:这是 DbContext 中的代码:
public DbSet<ClientBase> ClientBases { get; set; }
public DbSet<PropertyBase> PropertyBases { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}