Multiplicity constraint violated. The role 'Target' of the relationship '' has multiplicity 1 or 0..1."

时间:2015-09-14 15:30:47

标签: sql-server asp.net-mvc database entity-framework relational-database

I already found few similar questions on SO, but non of them really have the same case as mine. Basically I have two tables:

[Table("TestLoopStatistics")]
public class TestLoopStatisticsViewModel
{
    public int ID { get; set; }

    public int LoopNumber { get; set; }

    public virtual ICollection<TestTimeStatisticsItemViewModel> TestTimeStatisticsItemViewModels { get; set; }

    /// <summary>
    /// Reference to the parent class to create one to many relationship
    /// </summary>
    public TestStatisticsViewModel TestStatisticsViewModel { get; set; }

    /// <summary>
    /// Id of the parent class will be used as a foreign key
    /// </summary>
    public int TestStatisticsViewModelId { get; set; }
}

and

[Table("TestTimeStatisticsItems")]
public class TestTimeStatisticsItemViewModel
{
    public int ID { get; set; }

    public TestTimeItemType ItemType { get; set; }

    public string StartTime { get; set; }

    [Column(TypeName = "datetime2")]
    public DateTime StartTimeData { get; set; }

    /// <summary>
    /// Reference to the parent class to create one to many relationship
    /// </summary>
    public TestLoopStatisticsViewModel TestLoopStatisticsViewModel { get; set; }

    /// <summary>
    /// Id of the parent class will be used as a foreign key
    /// </summary>
    public int TestLoopStatisticsViewModelId { get; set; }
}

so class TestLoopStatisticsViewModel has One-To-Many relationship with TestTimeStatisticsItemViewModel, as simple as that.

However when I am trying to insert a new record into DB, I am getting:

Multiplicity constraint violated. The role 'TestTimeStatisticsItemViewModel_TestLoopStatisticsViewModel_Target' of the relationship 'MyProject.DAL.TestTimeStatisticsItemViewModel_TestLoopStatisticsViewModel' has multiplicity 1 or 0..1."

which is really confusing, I've already spent a day on it with no luck.

Thanks!

0 个答案:

没有答案