【问题标题】:ASP MVC 5: index or statistics with name 'IX_Id' already exists on tableASP MVC 5:表中已存在名称为“IX_Id”的索引或统计信息
【发布时间】:2014-03-09 12:40:55
【问题描述】:

我正在尝试创建一个包含 收货地址 字段和 帐单地址 字段的订单表。我认为创建一个 OrderAddress 模型然后将它关联两次会很聪明,一个用于送货地址,另一个用于帐单地址。

这就是我“设想”的方式。

我的对应模型

public class Order : BaseModel
{
    public Order()
    {
        this.OrderLines = new HashSet<OrderLine>();
    }
    public DateTime OrderDate { get; set; }
    public DateTime? DueDate { get; set; }

    public decimal Total { get; set; }

    public int DistributorProfileId { get; set; }
    public virtual DistributorProfile Distributor { get; set; }
    public int BillingAddressId { get; set; }
    public virtual OrderAddress BillingAddress { get; set; }
    public int ShippingAddressId { get; set; }
    public virtual OrderAddress ShippingAddress { get; set; }
    public virtual ICollection<OrderLine> OrderLines { get; set; }
}

public class OrderAddress : BaseModel
{
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }
    public virtual Order Order { get; set; }
}

但是,当我运行 Update-Database 时,我得到了这个错误:

操作失败,因为名称为“IX_Id”的索引或统计信息 表 'dbo.Orders' 上已存在。

我明白这意味着什么,所以我想为外键分配不同的名称。这可能吗?

奖金

这里可能存在设计缺陷吗?我应该以不同的方式创建模型吗?

【问题讨论】:

    标签: sql-server asp.net-mvc entity-framework ef-code-first


    【解决方案1】:
    I think if you can add a Key column like an Id to your Order address table:
    
    public class OrderAddress : BaseModel
    {
    pulbic int Id{get;set;}
    }
    
    and using model binder
    
    modelBuilder.Entity<Order>().HasOptional(x => x.BillingAddress).WithMany().HasForeignKey(u => u.BillingAddressId).WillCascadeOnDelete(false);
    
    modelBuilder.Entity<Order>().HasOptional(x => x.ShippingAddress).WithMany().HasForeignKey(u => u.ShippingAddressId).WillCascadeOnDelete(false);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 2014-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      相关资源
      最近更新 更多