Entity Framework Multiple Column as Primary Key by Fluent Api

喜你入骨 提交于 2019-11-29 09:02:34

Use anonymous type object instead of 2 separated statements:

    HasKey(pc => new { pc.ProductId, pc.CategoryId});

From MSDN: EntityTypeConfiguration.HasKey Method

If the primary key is made up of multiple properties then specify an anonymous type including the properties. For example, in C# t => new { t.Id1, t.Id2 } and in Visual Basic .Net Function(t) New From { t.Id1, t.Id2 }.

Frustratingly, particularly if you happen not to be fluent in lambda, the MSDN VB example is wrong... that should be a 'With', not a 'From'.

(Thanks Aki Siponen)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!