使用实体框架ASP.NET MVC应用程序更新相关的实体 [英] Updating related entities in an ASP.NET MVC application using Entity Framework

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

问题描述

让我们说我有两个班,一个Company类和Customer类。

Let's say I have two classes, a Company class and a Customer class.

public class Company
{
    public int Id {get; set;}
    public Customer Customer {get; set;}
}

public class Customer
{
    public int Id {get; set;}
    public string Name {get; set;}
}

我希望能够更新使用下拉列表中的公司相关联的客户。所以,在我的控制器编辑方法创建一个视图模型。

I want to be able to update the customer associated with the company using a drop down list. So, the edit method in my controller creates a viewmodel.

public class ViewModel
{
    public List<Customer> AvailableCustomers {get; set;}

    public Company Company {get; set;}
}

该视图创建了一个下拉列表,从中选择(使用淘汰赛)客户的形式。

The view creates a form with a drop down list from which to select a customer (using Knockout).

<select data-bind="attr: {name: 'Company.Customer.Id'}, options: AvailableCustomers(), optionsText: 'Name', optionsValue: 'Id', value: Company.Customer.Id"></select>

当的形式发布到服务器,我有由用户选择新的客户ID公司的模式。现在,我想用实体框架来更新数据库。

When the form is posted back to the server, I have the Company model with the new Customer Id selected by the user. Now I want to update the database using Entity Framework.

我得到的公司正在更新了使用的ID被调回表单数据的公司,这仅仅是我从早期的视图模型的数据库。现在我想用一个新客户来更新公司。我在这一点上的唯一信息是本公司的标识,用户从下拉列表中选择。所以我做这样的事情:

I get the Company being updated out of the database using the id for the Company that was posted back in the form data, which is just my viewmodel from earlier. Now I want to update the Company with a new Customer. The only information I have at this point is the Id of the Company that the user selected from the drop down list. So I do something like this:

var companyBeingUpdated = repository.GetByKey<Company>(Company.Id);
companyBeingUpdated.Customer.Id = Company.Customer.Id;

当我打电话更新我的资料库,我得到,说:财产ID是对象的关键信息的一部分,不能被修改异常。在我的存储库的更新方法如下:

As soon as I call update on my repository, I get an exception that says "The property 'Id' is part of the object's key information and cannot be modified". The update method on my repository looks like this:

    public void Update<TEntity>(TEntity entity) where TEntity : class
    {
        var entry = this._dbContext.Entry(entity);

        if (entry.State == EntityState.Detached)
        {
            this._dbContext.Set<TEntity>().Attach(entity);
        }

        entry.State = EntityState.Modified;
    }

我清楚地做错事与实体框架。我可以很容易地做到这一点与SQL语句,但我想知道我在做什么毛病EF

I am clearly doing something wrong with Entity Framework. I could easily do this with a SQL statement, but I want to understand what I am doing wrong with EF.

我如何去更新与EF与公司目标的实体的客户时,我有唯一信息是标识调回表单数据的客户?

How do I go about updating the Customer related entity of the Company object with EF when the only information I have is the Customer Id posted back in the form data?

我知道我大概可以检索使用id数据库中的客户实体并分配Company.Customer到检索客户,但其实这是一个很简单的例子。在现实中,我有不止一相关实体相当多的,如果我必须做出一个访问数据库来查找各相关实体,以做更新,我可以看到的性能迅速成为一个问题。

I know I could probably retrieve the Customer entity from the database using the Id and assign the Company.Customer to that retrieved Customer, but this is actually a very simplified example. In reality, I have quite a bit more than just one related entity and if I have to make a trip to the database to look up each related entity in order to do the update, I can see performance quickly becoming a problem.

推荐答案

要与实体做的是有机会获得外键。但是,您无法直接通过相关实体访问外键,而不是你在访问该相关实体的主键。

What you want to do with your entities is have access to the Foreign Key. however, you cannot directly access the Foreign Key through the related entity, instead you are accessing the Primary Key of that related entity.

即。而不是访问SQL中的公司 CUSTOMER_ID 属性,您正在访问的客户 编号属性。

i.e. instead of accessing the Company Customer_Id property in SQL, you are accessing the Customer Id property.

您可以暴露外键在实体框架模型,让你做你之后的更新类型。

You can expose the Foreign Keys to your model in Entity Framework, allowing you to do the types of updates you are after.

public class Company
{
    public int Id {get; set;}

    [ForeignKey("CustomerId")]
    public Customer Customer {get; set;}
    public int CustomerId {get;set;}
}

现在,你可以做的更新 companyBeingUpdated.CustomerId = Company.CustomerId; 并没有做一个查询检索完整的客户实体。

now, you can do updates as companyBeingUpdated.CustomerId = Company.CustomerId; and not have to do a lookup to retrieve the full Customer Entity.

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

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