Reputation: 8602
In SQL, I have a table with two columns that both map to the same primary key in another table. The easiest way to explain is using the table constraints:
ALTER TABLE [dbo].[sls_n_Promotion] WITH CHECK ADD CONSTRAINT [FK_SlsPromotion_SlsPromotionTradeSpendType] FOREIGN KEY([TradeSpendID])
REFERENCES [dbo].[sls_a_PromotionTradeSpendType] ([ID])
GO
ALTER TABLE [dbo].[sls_n_Promotion] CHECK CONSTRAINT [FK_SlsPromotion_SlsPromotionTradeSpendType]
GO
ALTER TABLE [dbo].[sls_n_Promotion] WITH CHECK ADD CONSTRAINT [FK_SlsPromotion_SlsPromotionTradeSpendType2] FOREIGN KEY([TradeSpendSubID])
REFERENCES [dbo].[sls_a_PromotionTradeSpendType] ([ID])
GO
ALTER TABLE [dbo].[sls_n_Promotion] CHECK CONSTRAINT [FK_SlsPromotion_SlsPromotionTradeSpendType2]
GO
I'm using database first, and I have created Promotion.partial.cs
. In this partial class, I'm struggling to get the Data Attributes set correctly for 'TradeSpendID' and 'TradeSpendSubID'. This is what I currently have:
[Display(Name="Trade Spend Type")]
[Column("TradeSpendID")]
[ForeignKey("PromotionTradeSpendType")]
int? TradeSpendId { get; set; }
[Display(Name = "Trade Spend Sub Type")]
[Column("TradeSpendSubID")]
[ForeignKey("PromotionTradeSpendType")]
int? TradeSpendSubId { get; set; }
When I go to create a Controller using scaffolding, I get the error:
Unable to determine a composite foreign key ordering for foreign key on type...
This isn't a composite foreign key.
Question: How do I use Data Attributes for the foreign keys on those 2 properties?
Upvotes: 0
Views: 48
Reputation: 89291
There should be two Navigation Properties. EG
[Display(Name="Trade Spend Type")]
[Column("TradeSpendID")]
[ForeignKey("PromotionTradeSpendType")]
public int? TradeSpendId { get; set; }
public virtual PromotionTradeSpendType PromotionTradeSpendType { get; set; }
[Display(Name = "Trade Spend Sub Type")]
[Column("TradeSpendSubID")]
[ForeignKey("PromotionTradeSpendSubType")]
public int? TradeSpendSubId { get; set; }
public virtual PromotionTradeSpendType PromotionTradeSpendSubType { get; set; }
Upvotes: 1