我有一个看起来像这样的属性:
public class CameraAttribute
{
public int Id { get; set; }
public int ProductId { get; set; }
public string CompatibleMemory { get; set; }
[MaxLength(255)] public string WhiteBalance { get; set; }
[MaxLength(255)] public string SceneModes { get; set; }
[MaxLength(255)] public string ShootingModes { get; set; }
[MaxLength(100)] public string PhotoEffects { get; set; }
[MaxLength(255)] public string CameraPlayback { get; set; }
public bool Tripod { get; set; }
public bool DirectPrinting { get; set; }
[MaxLength(50)] public string Colour { get; set; }
public CameraAttributePicture Picture { get; set; }
public CameraAttributeVideo Video { get; set; }
public CameraAttributeAudio Audio { get; set; }
public CameraAttributeBattery Battery { get; set; }
public CameraAttributeDimension Dimensions { get; set; }
public CameraAttributeDisplay Display { get; set; }
public CameraAttributeLightExposure Exposure { get; set; }
public CameraAttributeFlash Flash { get; set; }
public CameraAttributeFocusing Focusing { get; set; }
public CameraAttributeInterface Interface { get; set; }
public CameraAttributeLens Lens { get; set; }
public CameraAttributeNetwork Network { get; set; }
public CameraAttributeShutter Shutter { get; set; }
[ForeignKey("ProductId")] public Product Product { get; set; }
}
和音频看起来是这样的:
public class CameraAttributeAudio
{
public int Id { get; set; }
public int AttributeId { get; set; }
[MaxLength(50)] public string SupportedFormats { get; set; }
[ForeignKey("AttributeId")] public CameraAttribute Attributes { get; set; }
}
我已经在我的DbContext中设置了一些映射,如下所示:
modelBuilder.Entity<CameraAttribute>().HasRequired(m => m.Audio).WithRequiredPrincipal(m => m.Attributes).WillCascadeOnDelete(true);
但是,当我尝试运行命令add-migration时,出现此错误:
CameraAttribute_Audio_Target ::多重性在关系“ CameraAttribute_Audio”中的角色“ CameraAttribute_Audio_Target”中无效。因为从属角色属性不是关键属性,所以从属角色多重性的上限必须为'*'。
从Attribute类中可以看到,所有属性都会引发此错误。有谁知道为什么以及如何解决这个问题?
我认为问题在于CameraAttributeAudio
类也具有其自身的Id
属性,而在一对一关系中则是不必要的,因为它AttributeId
可以识别CameraAttribute
和两者CameraAttributeAudio
。它应该使用AttributeId
为主[Key]
。
public class CameraAttributeAudio
{
[Key]
[ForeignKey("Attributes")]
public int AttributeId { get; set; }
[MaxLength(50)]
public string SupportedFormats { get; set; }
public CameraAttribute Attributes { get; set; }
}
我将[ForeignKey]
属性移至属性,AttributeId
以便将注解放在一个位置。虽然在Attributes
属性上具有它也是正确的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句