简体   繁体   English

如何在实体框架中保存导航属性

[英]How To Save Navigation Properties in the Entity Framework

I'm trying to use the repository pattern to save an entity using the Entity Framework. 我正在尝试使用存储库模式来使用实体框架保存实体。 I'm unclear on how to save the Navigation Properties (like Account below). 我不清楚如何保存导航属性(如下面的帐户)。 Can anyone shed some light on this. 任何人都可以对此有所了解。 Especially how one would set the AccountId from an MVC controller all the way through to the repository where it's saved. 特别是如何将AccountId从MVC控制器一直设置到保存它的存储库。

Thanks! 谢谢!

--- Sample Code --- ---样本代码---

public void SavePerson(Person person)
    {           
        if (person != null)
        {
            using (xxxxxxEntities bbEntities = new xxxxxxEntities())
            {
                //see if it's in the db
                Person cPerson;

                ObjectQuery<Person> persons = bbEntities.Person;

                cPerson = (from p in persons
                         where p.PersonId == person.PersonId
                         select p).FirstOrDefault() ?? new Person();

                //synch it
                cPerson.Account.AccountId = person.Account.AccountId; //<=== ????
                cPerson.Active = person.Active;
                cPerson.BirthDay = person.BirthDay;
                cPerson.BirthMonth = person.BirthMonth;
                cPerson.BirthYear = person.BirthYear;
                cPerson.CellPhone = person.CellPhone;
                cPerson.CreatedBy = person.CreatedBy;
                cPerson.CScore = person.CScore;

Etc....

I think you may be going about this the hard way. 我想你可能会以艰难的方式解决这个问题。 There are lots of posts on the repository pattern, the way that works best with MVC is to get the item, then update it with the form, then save it. 存储库模式上有很多帖子,最适合MVC的方法是获取项目,然后使用表单更新它,然后保存它。 What you're doing is passing the item through to your repository, getting it again and then updating it with the object. 您正在做的是将项目传递到您的存储库,再次获取它,然后使用该对象更新它。

But that's not the problem you asked about; 但那不是你问过的问题;

cPerson.Account = (from a in Account
                   where a.AccountId.Equals(person.Account.AccountId)
                   select a).FirstOrDefault();

You need to set the Account object to an instance of the account you're trying to reference like this. 您需要将Account对象设置为您尝试引用的帐户的实例,如下所示。 You could, and probably should, extract this code into a seperate repository for the account, just make sure they share the same Entity context. 您可以并且可能应该将此代码提取到该帐户的单独存储库中,只需确保它们共享相同的实体上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架导航属性 - Entity framework Navigation Properties 视图上的实体框架导航属性 - Entity Framework Navigation Properties on Views 实体框架 - 导航属性不保存 - Entity Framework - Navigation Properties Not Saving 如何在属性上使用 ErrorMessage 并在没有它的情况下仍然使用 Entity Framework 保存 - How to use ErrorMessage on properties and still save with Entity Framework without it 实体框架-使用导航属性编辑多个实体 - Entity Framework - Edit multiple entities with navigation properties 使用实体框架的导航属性更新数据库 - Updating db using navigation properties of entity framework 实体框架6未加载导航属性(延迟加载) - Entity Framework 6 not loading Navigation Properties (lazy Loading) 将实体框架模型导航属性转换为DTO - Translating Entity Framework model navigation properties into DTOs 是否可以通过Entity Framework中的导航属性访问父实体? - It is possible to access a parent entity through navigation properties in Entity Framework? 处置上下文后,如何从实体框架(数据库优先)返回包含导航属性的模型? - How do I return a Model from Entity Framework (Database First) that includes the Navigation Properties after the context is disposed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM