我如何使用entry.GetDatabaseValues().ToObject()访问导航属性 [英] How can i accessa navigation property using entry.GetDatabaseValues().ToObject()

查看:84
本文介绍了我如何使用entry.GetDatabaseValues().ToObject()访问导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下操作方法:-

  [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Edit(Rack rack)
            {
                try
                {
                    if (ModelState.IsValid)
                    {
                        repository.InsertOrUpdateRack(rack);
                        repository.Save();
                        return RedirectToAction("Index");
                    }

                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var entry = ex.Entries.Single();
                    var databaseValues = (Rack)entry.GetDatabaseValues().ToObject();
                    var clientValues = (Rack)entry.Entity;


                    if (databaseValues.RU != clientValues.RU)
                        ModelState.AddModelError("RU", "Current value: "
                            + databaseValues.RU);
                    if (databaseValues.DataCenterID != clientValues.DataCenterID)
                        ModelState.AddModelError("DataCenterID", "Current value: "
                            + databaseValues.DataCenter.Name);
                    if (databaseValues.ZoneID != clientValues.ZoneID)
                        ModelState.AddModelError("ZoneID", "Current value: "
                            + databaseValues.Zone.Name);

但是,如果引发DbUpdateConcurrency异常,我将在 databaseValues.DataCenter.Name&上得到一个Null引用异常.databaseValues.Zone.Name .看来我无法从 Getdatabase().Toobject .

But if a DbUpdateConcurrncy exception is raised i will get a Null reference exception on databaseValues.DataCenter.Name & databaseValues.Zone.Name. It seems that i can not access the two navigation properties (Data-center & Rack) from the Getdatabase().Toobject.

有什么主意我可以解决这个问题吗?请记住,这两个导航值在数据库中已经具有值.

Any idea how i can solve this issue? baring in mind that the two navigation values already have values inside the databases.

推荐答案

我正离开这里,是为那些在尝试处理并发性问题或类似问题时仍然偶然发现此问题的人提供的.

I'm just leaving here for anyone who still stumbles upon this when trying to handle concurrency issues or the like.

ToObject 方法当前在使用最新的数据库值填充实体时,不会从数据库填充该实体的导航属性.这在某种程度上是可以接受的,因为导航属性可能具有应手动处理的添加/删除.另外请记住,实体是分离的

The ToObject method currently doesn't populate the navigational properties of the entity form the database when it populates the entity with the latest database values. This is somehow acceptable as the navigational properties may have additions/deletions that should be handled manually. Also bear in mind that the entity is detached

我还真不能够找到一种方法来由EF自动填充这些值.您可能必须对感兴趣的每个属性使用"GetValue"方法,然后自己填充它们.

I haven't really been able to find a way to populate those values automatically by the EF. You probably have to use the 'GetValue` method for each property you are interested in and then populate them yourself.

这是一个类似伪代码的解决方案:

Here is a pseudo-code like solution:

DbEntityEntry entry = context.Entry(yourEntity);
//Gets latest values of entity from the database
var propertyValues = entry.GetDatabaseValues();
//Fills an entity object with database values(except navigational properties)
dynamic newEntity = propertyValues.ToObject();
//Manually loading the needed navigational properties and handling add/removes.
foreach (var prop in propertyValues.PropertyNames)
{
    //either a collection<int> or an int.
    var idOfReferencedElement = propertyValues.GetValue<int>(foreignKeyField);
    //remember to handle add/removes too.
    newEntity.[thePropertyYouKnowExist] = context.[theDBSet].Find(idOfReferencedElement);
}

这篇关于我如何使用entry.GetDatabaseValues().ToObject()访问导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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