2016-05-03 11 views
0

私はこのようになります属性を持っている:EF 1 1に対して必要な関係

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; } 
} 

オーディオは次のようになります。私は私のDbContextでいくつかのマッピングを設定している

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; } 
} 

このように:

modelBuilder.Entity<CameraAttribute>().HasRequired(m => m.Audio).WithRequiredPrincipal(m => m.Attributes).WillCascadeOnDelete(true); 

ただし、コマンドを実行しようとすると、アドオン移行を、私はこのエラーを取得する:

CameraAttribute_Audio_Target: : Multiplicity is not valid in Role 'CameraAttribute_Audio_Target' in relationship 'CameraAttribute_Audio'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

あなたはすべてのプロパティは、このエラーをスローAttributeクラスから見ることができるように。 誰もがそれを解決する理由と方法を知っていますか?

答えて

1

私はこの問題は、AttributeIdCameraAttributeCameraAttributeAudioの両方を識別することができますので、それは1対1の関係では不要である一方、CameraAttributeAudioクラスが同様に独自のId性質を持っていることだと思います。これは、としてAttributeIdを使用する必要があります。

public class CameraAttributeAudio 
{ 
    [Key] 
    [ForeignKey("Attributes")] 
    public int AttributeId { get; set; } 

    [MaxLength(50)] 
    public string SupportedFormats { get; set; } 

    public CameraAttribute Attributes { get; set; } 
} 

注釈が一つの場所にあるように、私はAttributeIdプロパティに[ForeignKey]属性を移動しました。 Attributesプロパティでそれを持つことも正しいですが。

関連する問題