Unhandled Exception after Upgrading to Entity Framework 4.3.1

前端 未结 5 798
无人共我
无人共我 2020-12-06 17:33

Error:

Unhandled Exception: System.Data.SqlClient.SqlException: The operation failed because an index or statistics with name \'IX_ID\' already exi

5条回答
  •  眼角桃花
    2020-12-06 18:09

    As of EF 4.3, indexes are added for freign key columns during database creation. There is a bug that can cause an index to be created more than once. This will be fixed in a future EF release.

    Until then, you can work around the issue by creating your database using Migrations instead of database initializers (or the Database.Create() method).

    After generating the initial migration, you will need to delete the redundant call to Index().

    CreateTable(
        "dbo.PrivateMakeUpLessons",
        c => new
            {
                ID = c.Guid(nullable: false),
                ...
            })
        .PrimaryKey(t => t.ID)
        .ForeignKey("dbo.Lessons", t => t.ID)
        .ForeignKey("dbo.Cancellations", t => t.ID)
        .Index(t => t.ID)
        .Index(t => t.ID); // <-- Remove this
    

    To continue creating your database at run-time, you can use the MigrateDatabaseToLatestVersion initializer.

提交回复
热议问题