EF 4.3 Code First中的索引已存在错误,带有数据注释

时间:2012-05-03 19:38:36

标签: c# entity-framework data-annotations entity-framework-4.3

我正在尝试使用带有数据注释的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);
    }

1 个答案:

答案 0 :(得分:1)

修正了它。事实证明,我忘记了我创建了另一个与ClientBase有外键关系的课程。我忘了将它作为虚拟财产带入ClientBase,实体框架对我不满意。当我把它包括在内时,它运作得很好。

谢谢你们的帮助!

相关问题