1

我試圖創建一個計費數據庫與實體框架4.3使用Code First與數據註釋,並且我每次嘗試創建我的數據庫時出現錯誤。下面是我處理的對象:EF 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'. 

我見過的解決方案,與流利的這個問題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); 
    } 
+0

這一切都適合我。我不明白* table'Property'*來自哪個配置。 – tzerb

+0

Yikes!我忘記了將PropertyCase類放置在[Table(「Property」)]上。 – IronMan84

+0

能否在創建索引的SQL服務器上運行觸發器?我不知道它(甚至第一個)會從哪裏來。 – tzerb

回答

1

固定它。事實證明,我忘記了我有另一個與ClientBase有外鍵關係的類。我忘記把它作爲一個虛擬財產帶入ClientBase,實體框架對我不滿意。當我把它包括進來的時候,它完美地運作

謝謝你們的幫助!