Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug in Entity Framework 4.3 code first - index already exists

Everything worked fine in previous versions but now in 4.3 I get this error:

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'.


The User table has an Id property and is the primary key but I'm not using code or attributes anywhere else to create an additional index??

User Model:

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 */


}

It's failing here in my OnModelCreating(DbModelBuilder modelBuilder)

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

Update: just fixed it by using WithRequiredPrincipal instead of WithRequiredDependent. Not sure why this is different in 4.3

like image 991
Lee Smith Avatar asked Jun 16 '26 10:06

Lee Smith


1 Answers

This is a bug in EF 4.3. It will be fixed in EF5 RTM. For more information including a workaround see this question: Unhandled Exception after Upgrading to Entity Framework 4.3.1

like image 198
Arthur Vickers Avatar answered Jun 18 '26 01:06

Arthur Vickers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!