实体框架在 db.SaveChanges() 期间创建新数据行 [英] Entity Framework Creating new data rows during db.SaveChanges()

查看:22
本文介绍了实体框架在 db.SaveChanges() 期间创建新数据行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据本教程创建 AngularJS 应用程序:http://jphoward.wordpress.com/2013/01/04/end-to-end-web-app-in-under-an-hour/

Creating an AngularJS application based off of this tutorial: http://jphoward.wordpress.com/2013/01/04/end-to-end-web-app-in-under-an-hour/

课程:

public class Todo
{
    public int ID { get; set; }
    public virtual Status Status { get; set; }
}

public class Status
{
    public int ID { get; set; }
    public string Type { get; set; }
}

功能是您单击一个按钮并更改状态.单击该按钮后,所有正确的内容都会传递到 Visual Studio.最初它根本没有更新.经过一些研究,我找到了一些强制更改的方法,但随后在 db.SaveChanges() 中,它向具有相同类型"的状态添加了一个新行,只是从最后一个所在的位置增加了一个 ID.

Functionality is that you click a button and it changes the status. When the button is clicked, all the right things are being passed in to Visual Studio. Originally it wasn't updating at all. After some research I found some ways to force the changes, but then at db.SaveChanges() it adds a new row to Status that has the same 'Type', just an incremented ID from whatever the last one is at.

调用更新的JS:

Api.Todo.update({ id: todoID }, todo, function () {
    $location.path('/');
});

在此功能上遇到 VS:

Which hits VS on this function:

private DataContext db = new DataContext();

// PUT api/Todo/5
HttpResponseMessage PutTodo(int id, Todo todo)
{
    if (!ModelState.IsValid)
    {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
    }

    if (id != todo.ID)
    {
        return Request.CreateResponse(HttpStatusCode.BadRequest);
    }

    // Found a stack overflow that mentioned that you need to check for things already being tracked
    var entry = db.Entry(todo);

    if (entry.State == EntityState.Detached)
    {
        var set = db.Set<Todo>();
        Todo attachedEntity = set.Find(todo.ID);  // You need to have access to key
        if (attachedEntity != null)
        {
            // The following code does not update any changes to the foreign keys
            var attachedEntry = db.Entry(attachedEntity);
            attachedEntry.CurrentValues.SetValues(todo);
            db.Entry(attachedEntity).State = EntityState.Modified;

            // When this didn't work, I tried just changing the status on the already attached entity
            //attachedEntity.Status = todo.Status;
            //db.SaveChanges();
            // However when it hit SaveChanges() it created a new row in the Status table.
        }
        else
        {
            //This code was never hit
            entry.State = EntityState.Modified; // This should attach entity
        }
    }

    try
    {
        db.SaveChanges();
    }
    catch (DbUpdateConcurrencyException ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
    }

我的能力快用完了,希望得到一些帮助或指导.

I'm nearing the end of my capabilities and would love a little help or direction.

推荐答案

public class Todo
{
    public int ID { get; set; }

    //Foreign key
    public int StatusID { get; set; } //<====Add this line

    public virtual Status Status { get; set; }
}

发布数据时更新外键属性,而不是导航属性

When you post your data update the foreign key property,not the navigational property

另外,检查这个:为什么实体框架将现有对象重新插入到我的数据库中?http://msdn.microsoft.com/en-us/magazine/dn166926.aspx

Also ,check this: Why Does Entity Framework Reinsert Existing Objects into My Database? http://msdn.microsoft.com/en-us/magazine/dn166926.aspx

这篇关于实体框架在 db.SaveChanges() 期间创建新数据行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆