Enitiy框架更新相关实体 [英] Enitiy framework update related entities

查看:187
本文介绍了Enitiy框架更新相关实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在更新相关实体时遇到问题。所以我的Document实体有一组Customers。当我添加一个新文档时,我可以添加尽可能多的客户,但是当我更新时,只更新标量属性。



这是我更新文档的代码...

Hello I have a problem when updating related entities. So my Document entity has a collection of Customers. When Im adding a new document I can add as many customers as I want, but when Im updating only scalar properties are updated.

This is my code for updating a document...

public static bool UpdateDocument(Document documentToUpdate)
{
    using (MyDbContext = new MyDbContext())
    {
        // attaching the entity does not help
        if (context.Entry(documentToUpdate).State == EntityState.Detached)
            context.Documents.Attach(documentToUpdate);

        context.Entry(documentToUpdate).State = EntityState.Modified;
        context.SaveChanges();
        return true;
    }
}





关于文档操作,添加和删除客户的废话是在另一个层/项目中完成的,所以我的问题是我如何继续这个?或者如何在此方法中更新相关的客户集合?或者shuld我使用不同的方法只是为了客户操作。



类似





Evrything regarding document manipulation, adding and removing customers is done in another layer/project, so my question is how shuld I proceed with this? Or how do I update the related customer collection here in this method? Or shuld I use a different method just for customer manipulation.

Something like

public static void UpdateCustomersOnDoc(Document doc)
{
    using (MyDbContext context = new MyDbContext())
    {
        Document tempDoc = DocumentCRUD.GetByID(doc.Id);
        // remove the customers from the document (database)
        tempDoc.Customers.Clear();

        // add customers from the modified document
        foreach(var cust in doc.Customers)
        {
            docTemp.Customers.Add(cust);
        }
        // save changes to the context
        context.SaveChanges();
    }
}

推荐答案

问题是您正在尝试更新来自您的Document对象查看,这不行,因为它只是您数据库中文档的副本;并且删除和重新分配客户的每个文档更新都不行。



因此,在您的控制器类中,您应首先使用文档ID(或其主键)搜索当前文档,然后更新此对象(从数据库上下文)包含来自视图的数据(从您的方法在Document param中缓存的用户输入),最后将其保存到数据库中。这应该可以解决你的问题!



所以你应该有:



The problem is that you are trying to update the Document object that come from your view, and this is not OK, because it just a copy of your document from your database; and also the deleting and the reassigning the customers for each document update is not OK.

So in your controller class you should first search search by using Document ID (or its primary key) the current document, then update this object (that was read from the database context) with data that come from the view (user input that is cached in Document param from your method) and finally save it to the database. This should solve your problem!

So you should have:

public static bool UpdateDocument(Document documentToUpdate)
{
    using (MyDbContext = new MyDbContext())
    {
        Document doc = MyDbContext.FirstOrDefault(d=> d.ID == documetToUpdate.ID)
        //
        //
        CopyUserInputData(documentToUpdate, doc); //You should implement this method that save user input in the given "doc" object!   
        context.SaveChanges();
        return true;
    }
}


这篇关于Enitiy框架更新相关实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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