简体   繁体   English

无法定义两个对象之间的关系-为什么?

[英]The relationship between the two objects cannot be defined - why?

I am submitting a form that contains a user id and a pipe-delimited set of numbers. 我正在提交一个包含用户ID和以竖线分隔的数字集的表单。

The model looks like this: 该模型如下所示:

public class SeasonPassData
{
    public int UserID { get; set; }
    public string SpaceNumbers { get; set; }
}

That is getting handed off to this controller: 即将移交给该控制器:

[HttpPost]
public ActionResult SignUp(SeasonPassData data)
{
    var user = db.Users.Find(data.UserID);
    List<SeasonPass> passes = new List<SeasonPass>();

    char[] delimiter = { '|' };
    var numbers = data.SpaceNumbers.Split(delimiter).Select(n => Convert.ToInt32(n)).ToArray();
    foreach(int number in numbers)
    {
        passes.Add(new SeasonPass { SpaceNumber=number, User = user });
    }
    passes.ForEach(p => db.SeasonPasses.Add(p));
    db.SaveChanges();

    return RedirectToAction("Confirmation", "Home");
}

And should be creating and saving SeasonPasses: 并且应该创建并保存SeasonPasses:

public class SeasonPass
{
    public int ID { get; set; }
    public int SpaceNumber { get; set; }

    public virtual User User { get; set; }
}

However, this - passes.ForEach(p => db.SeasonPasses.Add(p)); 但是,这是passes.ForEach(p => db.SeasonPasses.Add(p)); keeps raiding this exception: 不断突袭此异常:

An exception of type 'System.InvalidOperationException' occurred in System.Data.Entity.dll but was not handled in user code System.Data.Entity.dll中发生类型为'System.InvalidOperationException'的异常,但未在用户代码中处理

Additional information: The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects. 附加信息:由于两个对象被附加到不同的ObjectContext对象,因此无法定义它们之间的关系。

How do I fix that? 我该如何解决?


I tried it with these changes based on a comment. 我根据评论对这些更改进行了尝试。 It still doesn't seem to work... 它似乎仍然不起作用...

[HttpPost]
public ActionResult SignUp(SeasonPassData data)
{
    using(var context = new TTUContext())
    {
        var user = context.Users.Find(data.UserID);
        List<SeasonPass> passes = new List<SeasonPass>();

        char[] delimiter = { '|' };
        var numbers = data.SpaceNumbers.Split(delimiter).Select(n => Convert.ToInt32(n)).ToArray();
        foreach (int number in numbers)
        {
            passes.Add(new SeasonPass { SpaceNumber = number, User = user });
        }
        foreach (var pass in passes)
        {
            context.SeasonPasses.Add(pass);
        }
        context.SaveChanges();
    }

    return RedirectToAction("Confirmation", "Home");
}

I'm surprised that what you are doing does not work, but here is a way to sidestep the issue. 令您惊讶的是,您正在执行的操作不起作用,但是这是避免该问题的一种方法。

Add public int UserID { get; set; } 添加public int UserID { get; set; } public int UserID { get; set; } public int UserID { get; set; } to SeasonPass and set it to assign the relationship instead of setting User . public int UserID { get; set; }设置为SeasonPass并将其设置为分配关系,而不是设置User

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法定义两个对象之间的关系 - The relationship between the two objects cannot be defined 实体框架v4 - 无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象 - Entity Framework v4 - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 无法定义两个对象之间的关系,因为它们已附加到不同的ObjectContext对象 - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 实体框架:“无法定义两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象。” - Entity Framework: “The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.” 无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象Entity Framework - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects Entity Framework 无法定义两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象 - The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects “无法定义两个对象之间的关系,因为它们被附加到不同的ObjectContext对象。” -错误 - 'The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.' - Error 引发异常:因为两个对象附​​加到不同的ObjectContext对象,所以无法定义它们之间的关系 - Exception thrown: The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects EF4错误:无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象 - EF4 error:The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects 调用.Save()“无法定义两个对象之间的关系”时,UnitofWork + DependencyResolver将引发异常。 - UnitofWork + DependencyResolver will raise an exception when calling .Save() “The relationship between the two objects cannot be defined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM