【问题标题】:Conflicting changes to the role x of the relationship y have been detected in web.api在 web.api 中检测到关系 y 的角色 x 的冲突更改
【发布时间】:2016-07-14 15:22:01
【问题描述】:

我意识到有几个类似的问题(例如 herehere),但我似乎无法将他们的解决方案应用于我的具体问题。

我有这些课程:

个人资料

public class ProfileModel
{
    public ProfileModel()
    {
        Resources = new List<OptionsModel>();
    }
    [Key]
    public  int BID { get; set; }
    public string Update_date { get; set; }
    public string Description { get; set; }
    public byte[] Picture_file { get; set; }
    public string Contact { get; set; }

    public ICollection<OptionsModel> Resources { get; set; }
    public virtual MissionModel Mission { get; set; }

}

任务模型

 public class MissionModel
    {
     [ForeignKey("Business")]
     public int MissionModelId { get; set; }
          ....more properties...
     public virtual ProfileModel Business { get; set; }
    }

以及资源

   public class OptionsModel
    {
        [Key]
        public int Resource_Id{ get; set; }
        public string Resource_name { get; set; }
        public string Resource_description { get; set; }

        //profile
        public virtual ProfileModel Business { get; set; }

        //all other config
        public virtual AccessModel Access { get; set; }
        public virtual GoalModel Goal { get; set; }
        public virtual MissionModel Mission { get; set; }
        public virtual SettingModel Setting { get; set; }
    }

这是一个 Web API 应用程序,当我使用 Json 发帖时,我收到以下错误:

“ExceptionMessage”:“已检测到对关系 'filament.Models.MissionModel_Business' 的角色 'MissionModel_Business_Target' 的冲突更改。”,

从我上面引用的第二个链接来看,它似乎正在尝试创建任务模型,但相同的模型似乎有所不同。我已经尝试了一些东西,但似乎没有一个效果足够好。例如,当我在关系的一端删除导航属性时,它可以工作(例如,它将数据保存到数据库中),但数据没有按预期正确组织。

非常感谢任何帮助!

【问题讨论】:

  • 显示型号代码是一个好的开始。我们还需要查看如何将这些添加到上下文并最终保存它们的简化代码
  • @CallumLinington 这是一个由 Visual Studio 生成的简单控制器。它将 ProfileModel 作为参数,对其进行验证,添加到上下文中,并保存更改。

标签: c# entity-framework asp.net-web-api


【解决方案1】:

第二个链接帮助我解决了问题。我只是将对象中的引用设置为空,然后实体正确地映射它们。就我而言,这看起来像这样:

foreach(var a in profileModel.Resources)
            {
                a.Business = null;
                a.Access.Resource = null;
                a.Goal.Resource = null;
                a.Setting.Resource = null;

            }

            profileModel.Mission.Business=null; 

我不确定这是否是正确的做法,但它确实有效。

【讨论】:

    猜你喜欢
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    相关资源
    最近更新 更多