关系模型设置

时间:2014-01-02 14:21:31

标签: c# asp.net-mvc model

如果已经回答,请原谅我,我真的找不到我需要做的解释/示例。

我有两个模型,Dealer模型和Location模型。创建Location后,必须为Dealer创建DealerShipToLocationBillToLocationLocations与之关联,但Dealer Dealer Locations也可能为Dealer创建public class Dealer : BaseEntity { [Key] public int DealerId { get; set; } public string DealerName { get; set; } public virtual Location ShipToLocation { get; set; } public virtual Location BillToLocation { get; set; } } public class Location : BaseEntity { [Key] public int LocationId { get; set; } public string LocationStateProvince { get; set; } public string LocationCountry { get; set; } //public int DealerId { get; set; } //[ForeignKey("DealerId")] public virtual Dealer Dealer { get; set; } } 目前与...有关。我需要设置模型,以便我可以为The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Dealers_dbo.Locations_ShipToLocation_LocationId". The conflict occurred in database "CoinWeb", table "dbo.Locations", column 'LocationId'. 创建{{1}}的下拉列表,用户可以选择将其添加为“当前”ShipTo或BillTo位置。我希望我能正确解释......

以下是我的模型设置,但我收到了外键约束错误:

{{1}}

我运行数据库迁移时遇到的当前错误是:

{{1}}

提前感谢您的帮助!!

1 个答案:

答案 0 :(得分:0)

我认为以下陈述适用于您的案例

客户有许多位置和位置可以被许多客户共享。所以我认为关系是M --- M所以创建一个名为CustomerLocations的新模型

public class CustomerLocations: BaseEntity
    {
       [Key]
       public int ID { get; set; }

      public Customer Customer {get;set;}
       public virtual LocationType LocationType { get; set; }
       public virtual Location Location { get; set; }
    }

public enum LocationType{

Billing=1,
Shipping=2
}