更新实体对象及其更新的导航属性

时间:2013-09-15 07:37:23

标签: c# asp.net-mvc entity-framework entity-framework-4.1 ef-database-first

我在asp.net mvc应用程序中使用EF的数据库第一种方法,我已经生成了类似这样的实体类..

public class BenchmarkRange
{
    public int ID { get; set; }
    public int GoodForm { get; set; }
    public int GoodTo { get; set; }
    //...
    public ICollection<PerformanceScores> PerformanceScores { get; set; }
}

public class PerformanceScores
{
    public int ID { get; set; }
    public int BenchmarkRangeID { get; set; }
    public int RangeFrom { get; set; }
    public int RangeTo { get; set; }
    public int Score { get; set; }

    public BenchmarkRange BenchmarkRange { get; set; }
}

现在,我发送BenchmarkRange对象进行查看..

在客户端进行了一些编辑,我通过默认模型绑定得到了模型,它将有PerformanceScores一些已编辑,一些新添加,一些从集合中删除。

如何使用更新的BenchmarkRange更新PerformanceScores

其实我不知道如何做到这一点。我尝试从数据库中获取相同的对象并将其与新数据进行比较,但它的抛出错误类似于..multiple objects with same key cannot be tracked by the ObjectstateManager..

可以请别人帮忙吗?

1 个答案:

答案 0 :(得分:0)

您需要创建一个新类(viewModel)并将两个实体放在该类中。您可以在此处查看更多详细信息以及完整示例...

Getting error on POST with Entity Framework - Value cannot be null. Parameter name: source