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.
我希望能够使用下拉列表更新与公司相关的客户。我的控制器中的edit方法创建了一个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).
视图使用下拉列表创建一个表单,从中选择一个客户(使用Knockout)。
<select data-bind="attr: {name: 'Company.Customer.Id'}, options: AvailableCustomers(), optionsText: 'Name', optionsValue: 'Id', value: Company.Customer.Id"></select>
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:
我将公司从数据库中更新出来,使用的是在表单数据中发布的公司id,这只是我之前的viewmodel。现在我想给公司换一个新客户。此时,我仅有的信息是用户从下拉列表中选择的公司Id。我这样做:
var companyBeingUpdated = repository.GetByKey<Company>(Company.Id);
companyBeingUpdated.Customer.Id = Company.Customer.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:
当我调用存储库上的update时,就会出现一个异常,说“属性‘Id’是对象关键信息的一部分,不能修改”。我的存储库中的更新方法如下:
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;
}
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.
我显然在实体框架上做错了什么。我可以很容易地使用SQL语句来实现这一点,但是我想知道我在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在表单数据中返回时,我如何使用EF更新公司对象的客户相关实体?
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.
我知道我可以使用Id从数据库中检索客户实体并分配公司。客户到检索到的客户,但这实际上是一个非常简单的例子。实际上,我拥有的不仅仅是一个相关的实体,如果我需要访问数据库来查找每个相关的实体以进行更新,我可以看到性能很快就会成为一个问题。
1
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.
您要对实体做的是访问外键。但是,不能通过相关实体直接访问外键,而是访问相关实体的主键。
i.e. instead of accessing the Company
Customer_Id
property in SQL, you are accessing the Customer
Id
property.
例如,您访问的不是SQL中的Customer_Id属性,而是客户Id属性。
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;}
}
now, you can do updates as companyBeingUpdated.CustomerId = Company.CustomerId;
and not have to do a lookup to retrieve the full Customer
Entity.
现在,您可以在companybeingupdates时进行更新。CustomerId = Company.CustomerId;并且不必进行查找以检索完整的客户实体。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/01/04/40ed36d98106ae6586d597034531c0ac.html。