The operation failed because an index or statistics with name 'IX_AuctionId' already exists on table 'Bids'

This question is related to this question which was never answered. I have a more real world example here though - so hoping for some help.

I have an auction, bid and auctiondetail(a flattened table) class. I am trying to include my bids in the auction as well as auctiondetail table, auction and auctiondetail have the same PK

using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;

namespace OnCall.VirtualWarehouse.Data.Models.Auctions
{
    using System;

    public class AuctionCopy
    {
        public Guid AuctionCopyId { get; set; }

        public virtual Collection<BidCopy> BidCopies { get; set; }

    }

    public class BidCopy
    {
        public Guid BidCopyId { get; set; }

        public Guid AuctionCopyId { get; set; }

        public AuctionCopy AuctionCopy { get; set; }


    }

    public class AuctionDetailCopy
    {
        [Key]
        public Guid AuctionCopyId { get; set; }

        public virtual Collection<BidCopy> BidCopies { get; set; }
    }

}

Here's my DBContext:

public class DataContext : DbContext
{
    static DataContext()
    {
        Database.SetInitializer(new DropCreateIfChangeInitializer());
    }

    public DataContext()
    {
        Configuration.ProxyCreationEnabled = false;
    }

    public IDbSet<AuctionCopy> AuctionCopy { get; set; }

    public IDbSet<BidCopy> BidCopy { get; set; }

    public IDbSet<AuctionDetailCopy> AuctionDetailCopy { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }

    public void Seed(DataContext context)
    {

    }
}

When generating the database, I get

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

Any ideas how I can get this to work?

Answers


The bug come from EF4.3 (and 4.3.1). It should be fixed for the next release.

Have a look on Unhandled Exception after Upgrading to Entity Framework 4.3.1. They purpose a workaround using migration system.


Need Your Help

Rspec: ActionMailer::Base.deliveries should be empty

ruby-on-rails ruby rspec capybara

Testing filling in the contact form without filling in one of the required fields in one test and then filling in the honey_pot field in another test. The tests when valid info is filled in are pas...