实体框架与相关实体更新 [英] Entity Framework Updating with Related Entity

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

问题描述

我使用的是EF尝试更新ASP.NET的实体。我创建一个实体,设置它的属性然后用ID传递给它回EF在一个单独的层,可以应用的变化。我这样做,因为我只能存储实体的ID,当它被绑定到UI控件。

I'm using the EF to try to update an entity with ASP.NET. I'm creating an entity, setting it's properties then passing it back to the EF on a separate layer with the ID so the change can be applied. I'm doing this because I only store the ID of the entity when it's been bound to the UI controls.

一切工作的标准属性,但我不能更新产品的Category.ID(相关实体)。我试过的EntityKey,的EntityReference和其他几个,但类别ID不会被保存。这是我有:

Everything works for standard properties, but I can't update the Category.ID of a Product (a related entity). I've tried EntityKey, EntityReference and a few other but the category ID isn't saved. This is what I have:

Product product = new Product();
product.CategoryReference.EntityKey = new EntityKey("ShopEntities.Categories", "CategoryID", categoryId);
product.Name = txtName.Text.Trim();
... other properties
StockControlDAL.EditProduct(productId, product);

public static void EditProduct(int productId, Product product) {
 using(var context = new ShopEntities()) {
     var key = new EntityKey("ShopEntities.Products", "ProductID", productId);
     context.Attach(new Product() { ProductID = productId, EntityKey = key });
     context.AcceptAllChanges();
     product.EntityKey = key;
     product.ProductID = productId;
     context.ApplyPropertyChanges("ShopEntities.Products", product);
     context.SaveChanges();
 }
}

我真的想使用EF但我似乎是具有与使用它与ASP.NET一些问题。

I really want to use the EF but I seem to be having a few problems with using it with ASP.NET.

推荐答案

这是公认的答案<一个href=\"http://stackoverflow.com/questions/899734/strongly-typed-asp-net-mvc-with-entity-framework\">http://stackoverflow.com/questions/899734/strongly-typed-asp-net-mvc-with-entity-framework

context.AttachTo(product.GetType().Name, product);
ObjectStateManager stateMgr = context.ObjectStateManager;
ObjectStateEntry stateEntry = stateMgr.GetObjectStateEntry(model);
stateEntry.SetModified();
context.SaveChanges();

你试过了吗?

[更新,在上面code不起作用]

这是我用这样下次code座是小更容易扩展属性就明白了:

This is small extension property I used so next code block is easier to understand:

public partial class Product
{
    public int? CategoryID
    {
        set
        {  
           CategoryReference.EntityKey = new EntityKey("ShopEntities.Categories", "CategoryID", value);
        }
        get
        {
            if (CategoryReference.EntityKey == null)
                return null;

            if (CategoryReference.EntityKey.EntityKeyValues.Count() > 0)
                return (int)CategoryReference.EntityKey.EntityKeyValues[0].Value;
            else
                return null;
        }
    }
}

和工作对我来说(这一次肯定):

and that worked for me (this time for sure):

System.Data.EntityKey key = new System.Data.EntityKey("ShopEntities.Products", "ProductID", productId);
        object originalItem;   

        product.EntityKey = key;
        if (context.TryGetObjectByKey(key, out originalItem))
        {
            if (originalItem is EntityObject &&
                ((EntityObject)originalItem).EntityState != System.Data.EntityState.Added)
            {
                Product origProduct = originalItem as Product;   
                origProduct.CategoryID == product.CategoryID;//set foreign key again to change the relationship status           
                context.ApplyPropertyChanges(
                    key.EntitySetName, product);

            }
        }context.SaveChanges();

有关确保它看起来哈克。我认为原因是因为EF关系具有地位的实体(修改,增加,删除),并基于该状态改变EF外键的值或删除行,如果多对多的关系是如此。出于某种原因(不知道为什么)的关系状态没有改变一样的财产状况。这就是为什么我不得不设置originalItem的CategoryReference.EntityKey为了改变这种关系的状态。

For sure it's looks hacky. I think that the reason is because the EF relationships have status as entities (modified, added, deleted) and based on that status EF changes the value of foreign keys or deletes row if many to many relationship is in case. For some reason (don't know why) the relationship status is not changed the same as property status. That is why I had to set the CategoryReference.EntityKey on originalItem in order to change the status of the relationship.

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

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