此問題與從未回答過的this question有關。這裏我有一個更真實的例子 - 所以希望得到一些幫助。操作失敗,因爲表'Bids'上已經存在名爲'IX_AuctionId'的索引或統計信息
我有一個拍賣,競標和auctiondetail(flatten表)類。 我想包括我在拍賣會上出價和auctiondetail表,拍賣和auctiondetail具有相同的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; }
}
}
這裏是我的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)
{
}
}
當生成數據庫,我得到
The operation failed because an index or statistics with name 'IX_AuctionId' already exists on table 'Bids'.
任何想法,我可以得到這個工作?
是否標記「Key」屬性[NotMapped]不能解決問題?爲什麼你需要重寫附加屬性「Key」? – kingdango
感謝您的評論。我嘗試徹底刪除關鍵屬性,因爲它與問題沒有任何關係。更新了上面的問題以反映這一變化。即使刪除了關鍵屬性,問題仍然存在。 –
您是否在完成之後刷新數據庫中的模式?嘗試將模式推入空數據庫。 – bluevector