一对多关系中的多重性错误

时间:2016-12-08 15:39:12

标签: c# entity-framework

我有多个实体嵌套在一起,当我尝试添加调查类时,我收到错误

  

违反了多重性约束。角色' Response_Pages_Source'关系&SurveyCononkeyAPIv3.Response_Pages'具有多重性1或0..1。

因为他们都继承了实体

public abstract class Entity  :IEquatable<Entity>
{

    [DataMember]
    public virtual long Id { get; set; }

    public override int GetHashCode()
    {
        return Convert.ToInt16(Id);
    }

    public bool Equals(Entity other)
    {
        return false;
    }
}

调查实体

public class Survey : Entity, IEquatable<Survey>
{
//More fields//
    [DataMember]
    public string AnalyzeUrl { get; set; }


    [DataMember]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public override long Id { get; set; }

    [DataMember]
    public string Language { get; set; }

    [DataMember]
    public int? PageCount { get; set; }
    [DataMember]
    public virtual List<Page> Pages { get; set; }
    [DataMember]
    public string Preview { get; set; }
    //More Fields
    public bool Equals(Survey other)
    {
        return this.Id == other.Id;
    }
}

和Page

public class Page : Entity, IEquatable<Page>
{
    [DataMember]
    public string Description { get; set; }

    [DataMember]

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public new long? Id { get; set; }

    [DataMember]
    public int? Position { get; set; }

    [DataMember]
    public int? QuestionCount { get; set; }
   //More Objects

    // public long SurveyId { get; set; }
    // public List< Survey> Survey   { get; set; }
    //tried including these , can't set in my automappers

但我找不到解决此问题的方法。

增加:

指向响应实体,后面是

public class Response : Entity, IEquatable<Response>
{
    [DataMember]
    public string AnalyzeUrl { get; set; }

    [DataMember]
    public CollectionMode? CollectionMode { get; set; }

    [DataMember]
    public long? CollectorId { get; set; }

    [DataMember]
    public string CustomValue { get; set; }

    //[DataMember]
    //public Dictionary<string, string> CustomVariables { get; set; }

    [DataMember]
    public DateTime? DateCreated { get; set; }

    [DataMember]
    public DateTime? DateModified { get; set; }

    [DataMember]
    public string EditUrl { get; set; }

    [DataMember]

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public new long? Id { get; set; }

    [DataMember]
    public string IpAddress { get; set; }

    //[DataMember]
    //public Dictionary<string, object> LogicPath { get; set; }

    //[DataMember]
    //public Dictionary<string, object> Metadata { get; set; }
    [DataMember]
    public virtual List<object> PagePath { get; set; }

    [DataMember]
    public virtual List<ResponsePage> Pages { get; set; }
    [DataMember]
    public virtual List<ResponseQuestion> Questions { get; }

    [DataMember]
    public long? RecipientId { get; set; }

    [DataMember]
    public ResponseStatus? ResponseStatus { get; set; }

    [DataMember]
    public long? SurveyId { get; set; }

    [DataMember]
    public int? TotalTime { get; set; }

    public bool Equals(Response other)
    {
        return Id == other.Id;
    }
}

1 个答案:

答案 0 :(得分:1)

标记Survey属性(来自Page),实际上你需要添加一个新属性:

[ForeignKey("SurveyId")]
[DataMember]
public Survey Survey {get;set;}

来自Survey with:

的页面列表
 [InverseProperty(nameof(Page.Survey))]