MVC3添加,删除,添加 - DbUpdateException

时间:2013-03-11 17:53:24

标签: sql-server database asp.net-mvc-3

我有两个具有多对多关系的模型。当我向UserProfile添加Event然后删除它,然后尝试再次添加它我在浏览器中收到此错误:

Violation of PRIMARY KEY constraint 'PK_dbo.UserProfileEvents'. Cannot insert duplicate key in object 'dbo.UserProfileEvents'. The duplicate key value is (17, 1).
The statement has been terminated

在Visual Studio的调试模式中,我得到了

DBUpdateException

An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.

当我在添加之前调试..在Events UserProfile列表中确实没有任何内容,并且UserProfile事件列表中没有事件。在我添加SaveChanges()后,两个列表中都包含正确的实体。在我移除它之前仍然存在之后我移除它并不存在两者都是好的。 当我再次添加时...事件UserProfile列表中没有任何内容,并且UserProfile事件列表中没有任何事件应该如此,但在SaveChanges()调用后我得到了这些错误。

它几乎就像从模型中删除它而不是在实际的数据库表中删除它?

模型

public class Event
{
    public Event()
    {
        this.UserList  = new HashSet<UserProfile>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime Time { get; set; }
    public int MaxCapacity { get; set; }
    public ICollection<UserProfile> UserList { get; set; }
}



public class UserProfile
{
    public UserProfile()
    {
        this.EventList = new HashSet<Event>();
    }

    public int ID { get; set; }
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public ICollection<Event> EventList { get; set; }
}

}

public class Database : DbContext
{
    public DbSet<Event> Events { get; set; }
    public DbSet<UserProfile> UserProfiles { get; set; }
}

活动控制器

    public ActionResult Index()
    {
        if (!User.Identity.IsAuthenticated)
        {
            return RedirectToAction("Index", "Home");
        }

        if (!User.IsInRole("Admin"))
        {
            return UserIndex();
        }

        List<Event> list = db.Events.ToList();

        return View(db.Events.ToList());
    }


    public ActionResult UserIndex()
    {
        UserProfile myProfile = new UserProfile();

        foreach (UserProfile profile in db.UserProfiles)
        {
            if (profile.UserName == User.Identity.Name)
            {
                myProfile = profile;
            }
        }

        ViewBag.UserProfile = myProfile;

        return View("UserIndex", db.Events.ToList());
    }


    public ActionResult Join(int id)
    {
        UserProfile newProfile = new UserProfile();

        foreach (UserProfile profile in db.UserProfiles.ToList())
        {
            if (profile.UserName == User.Identity.Name)
            {
                newProfile = profile;
                break;
            }
        }

        db.Events.Find(id).UserList.Add(newProfile);
        db.SaveChanges();

        //return RedirectToAction("UserIndex");
        return UserIndex();
    }


    public ActionResult Leave(int id)
    {
        UserProfile newProfile = new UserProfile();

        foreach (UserProfile profile in db.UserProfiles.ToList())
        {
            if (profile.UserName == User.Identity.Name)
            {
                newProfile = profile;
                break;
            }
        }
        db.Events.Find(id).UserList.Remove(newProfile);
        db.SaveChanges();

        //return RedirectToAction("UserIndex");
        return UserIndex();
    }

0 个答案:

没有答案