我想要一张桌子,上面有许多其他物品的图片,比如膳食、食品、...
一张图片是针对特定课程的(比如用餐)。通过这种方式,我可以拥有许多带有 PictureId 为 FK 的表,但无法应用此第二个数据注释属性。
public class Picture
{
[Key, ForeignKey("Ingridient"), ForeignKey("Meal")] //<---here I get error
public int PictureId { get; set; }
//properties
public byte[] PictureData { get; set; }
public string Name { get; set; }
[Required]
public virtual Ingridient Ingridient { get; set; } //1 - 1
[Required]
public virtual Meal Meal { get; set; } //1 - 1
}
我想我不遵守约定,但请告诉我如何解决它。为什么 PictureId 在许多表中不能是 FK(Meals、Ingridients 等)
我可以有很多表,PictureId 为 FK
然后那些其他实体应该具有外键属性和导航属性。
例如
public class Meal
{
public int MealId {get;set;}
// ...
public int? PictureId {get;set;}
public virtual Picture Picture {get;set;}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句