English 中文(简体)
如何绕过实体框架的验证制度。
原标题:How to bypass the System.Data.Entity.Internal.InternalPropertyEntry.ValidateNotDetachedAndInModel(String method) validation of Entity framework?

I m 采用一种定制的方法,追踪单个经修改的、与一层不相连的实体类别。 我从中摘取。

Programming Entity Framework: DbContext by Julia Lerman and Rowan Miller (O’Reilly). Copyright 2012 Julia Lerman and Rowan Miller, 978-1-449-31296-1.

该守则是:

public void ApplyChanges<TEntity>(TEntity root) where TEntity : class, IObjectWithState {
        // bind the entity back into the context
        dbContext.Set<TEntity>().Add(root);
        // throw exception if entity does not implement IObjectWithState
        CheckForEntitiesWithoutStateInterface(dbContext);

        foreach (var entry in dbContext.ChangeTracker.Entries<IObjectWithState>()) {
            IObjectWithState stateInfo = entry.Entity;
            if (stateInfo.State == RecordState.Modified) {
                // revert the Modified state of the entity
                entry.State = EntityState.Unchanged;
                foreach (var property in stateInfo.ModifiedProperties) {
                    // mark only the desired fields as modified
                    entry.Property(property).IsModified = true;
                }
            } else {
                entry.State = ConvertState(stateInfo.State);
            }
        }
        dbContext.SaveChanges();
    }

这种方法的目的是让欧洲武装部队只了解预先界定的一组实体领域,并准备在下届“拯救儿童”呼吁中更新。 这需要这样做,才能使实体在协会工作。 NET MVC 3 如下:

  • on initial page load: the Get action of the controller is loading the entity object and passing it as a parameter to the view.

  • The View generate controls for editing 2 of the fields of the entity, and holds the ID of the record in a hidden field.

  • When hitting [save] and posting the entity back to the controller all of the fields excepting the 3 preserved in the view comes with a null value. This is the default behavior of the MVC binding manager.

如果在数据库中不作改动,更新的查询当然会超越未绘制地图的田地,判决如下:

UPDATE non_mapped_field_1 = NULL, ..., mapped_field_1 =  mapped_value_1 , mapped_field_2 =  mapped_value_2 , ... non_mapped_field_n = NULL WHERE ID = mapped_field_3

这是试图逐个跟踪领域并仅更新那些有兴趣的领域的原因。 在与ApplyChanges()交接习惯方法之前 i m 添加一意欲列入向综合国家通报的实地清单。 修改 财产清单,以便作如下陈述:

UPDATE mapped_field_1 =  mapped_value_1 , mapped_field_2 =  mapped_value_2  WHERE id = mapped_value_3

问题在于,在ApplyChanges修改的一个领域,即:

entry.Property(property).IsModified = true;

该系统正在扔下以下<><<> > 除外>:

{System.InvalidOperationException: Member  IsModified  cannot be called for property  NotifyCEDeadline  on entity of type  User  because the property is not part of the Entity Data Model.

at System.Data.Entity.Internal.InternalPropertyEntry.ValidateNotDetachedAndInModel(String method)

at System.Data.Entity.Internal.InternalPropertyEntry.set_IsModified(Boolean value)

at System.Data.Entity.Infrastructure.DbPropertyEntry.set_IsModified(Boolean value)

...

因此,问题是。 绕过这种“EF”的验证,或者让人们了解这一系统财产的存在情况,即试图改变的不动产。

arch:

  • EF Code first (annotation + Fluent API)
  • Oracle .NET EF Data provider (ODAC)
  • Context is injected to a cutom business context with nInject.MVC => this is the reason i customized the ApplyChanges() method from

    using (var context = new BreakAwayContext()){ context.Set().Add(root);

    简单地呼吁已经初创的目录

    dbContext.Set(Root)

  • 甲骨质数据库是人工创建的,即没有EF的帮助,因此没有使用EF元数据表。

Thanks, Ivan.

问题回答

然而,我无法找到任何资料,说明你为何需要物品中称为“变造”的瞬间财产,以及(或)你为什么需要向欧洲法院说明正在修改的不动产(欧经会赢得一定能够坚持这种财产)。

变造财产的价值应由模范人确定,如果该财产在任何方面被包括在内。

您在申请中仅能加入法典。 变换法只能用入物过滤已知财产。 CurrentValues.PropertyNames,例如:

foreach (var property in stateInfo.ModifiedProperties) { 
    // mark only the desired fields as modified 
    if (entry.CurrentValues.PropertyNames.Contains(property)) {
        entry.Property(property).IsModified = true; 
    }
} 

Update: Ivan, very sorry I did not understand the problem better when you posted it several months ago and that I did not follow up after your added these clarifying comments. I think I understand better now. That said, I think the code snippet that I offered can be part of the solution. From looking at the exception you are getting again, I understand now that the problem that EF is detecting is that NotifyCEDDealine is not a persistent property (i.e. it is not mapped in the Code First model to a column in the database). IsModified can only be used against mapped properties, therefore you have two options: you change the code of the implementation of IObjectWithState in your entities so that non-mapped properties are not recorded in ModifiedProperties, or you use my code snippet to prevent calling IsModified with those.

这样做的替代办法是使用主计长。 TryUpdate Model 仅对贵实体中经修改的财产进行标注。

希望这一帮助(尽管我理解是很晚)。





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签