3

一切都在以前的版本中運行良好,但現在在4.3我得到這個錯誤:錯誤的實體框架4.3代碼第一 - 索引已經存在

An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.DLL but was not handled in user code

Additional information: The operation failed because an index or statistics with name 'IX_Id' already exists on table 'Users'.


user表中有一個ID屬性,是主要的鍵,但我沒有使用代碼或屬性在其他地方創建一個額外的索引?

用戶模型:

public class User 
{ 
    public int Id { get; set; } 

    public virtual Settings Settings { get; set; } /* 1-1 */ 

    public virtual Profile Profile { get; set; } /* 1-1 */ 
    public virtual Account Account { get; set; } /* 1-1 */ 


} 

它的失敗在我的OnModelCreating(DbModelBuilder模型構建器)

  modelBuilder.Entity<User>().HasRequired(u => u.Settings).WithRequiredDependent(); 
     modelBuilder.Entity<User>().HasRequired(u => u.Profile).WithRequiredDependent(); 
     modelBuilder.Entity<User>().HasRequired(u => u.Account).WithRequiredDependent(); 

更新:通過使用替代WithRequiredDependent WithRequiredPrincipal只是固定它。不知道爲什麼這是不同的4.3

+0

您能向我們展示用戶模型的代碼嗎? – JasCav 2012-02-23 00:37:27

+0

我有類似的問題。但.WithRequired(),.WithRequiredPrincipal()和.WithRequiredDependent()全都失敗。 – 2012-05-19 13:37:42

+0

這可能是這個問題的重複/答案:http://stackoverflow.com/questions/10438219/index-already-exists-error-in-ef-4-3-code-first-with-data-annotations – kingdango 2012-05-31 19:08:15

回答