EF 4.3中的索引已经存在错误,首先是数据注释 [英] Index already exists error in EF 4.3 Code First with Data Annotations

查看:147
本文介绍了EF 4.3中的索引已经存在错误,首先是数据注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Code First与Data Annotations创建一个账单数据库,并且每次尝试创建数据库时都会收到一个错误。以下是我正在处理的对象:

I'm trying to create a billing database with Entity Framework 4.3 using Code First with Data Annotations, and I'm getting an error every time I try to create my database. Here are the objects that I'm dealing with:

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; }
}

无论我尝试做什么,我总是得到这个错误: / p>

No matter what I try to do, I always get this error:

The operation failed because an index or statistics with name 'IX_ClientID' already exists on table 'Property'.

我已经用Fluent API看到了这个问题的解决方案,但是我没有找到一个数据注释。

I've seen solutions to this question with the Fluent API, but I haven't found one for Data Annotations.

有没有人知道如何解决这个问题?

Does anyone have any idea how to fix this?

编辑: DbContext:

Here is the code in the 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);
    }


推荐答案

原来我忘记了另外一个与ClientBase有外键关系的课程。我忘了把它作为虚拟财产带入ClientBase,实体框架对我并不满意。当我包含它,它的工作完美。

Fixed it. It turned out that I had forgotten that there was another class that I had made that had a Foreign Key relationship to ClientBase. I forgot to bring that into ClientBase as a virtual property and Entity Framework was not happy with me. When I included it, it worked perfectly.

谢谢你们的帮助!

这篇关于EF 4.3中的索引已经存在错误,首先是数据注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆