English 中文(简体)
Lazy / Detingedloading links not on time?
原标题:Lazy / Deferred loading of links not on time?

一个人是否经历过以下情况? 用提及其他实体的田地对物体进行鉴定,会给你留下一个错误,表明现场存在,如果你会破坏方案,你会检查这些田地<>are/strong>有人居住。

现在,我曾两次遇到过这种情况,似乎有些问题涉及zy装问题,因为如果 la装不给答案。

我们有这种(简化)模式。

class Survey {
  ...
  public bool Enabled {get; set;}
  [Required]
  public virtual OrganisationalUnit OU {get; set;}
  ...
}

如果我们只做<代码>Context.Surveys.Single(id)或。 调查。 如果(s => s.Id ==id), 更改<代码> 可使用<>条/条码>外地(或任何其他领域),并填写<条码>Context.SaveChanges(),则在10次中,有9次出现一个验证错误,即:OU field is required and that it s not present。

添加<代码>。 问题已经解决,我认为问题已经结束。 尽管我昨天再次遇到以下法典的类似问题:

public class SurveyQuestionMultipleChoiceMultiSelect : SurveyQuestionMultipleChoice
{
    public override IEnumerable<ValidationResult> validateValue(string _, IEnumerable<string> values)
    {
        int ivalue;
        foreach( string value in values) {

            bool success = int.TryParse(value, out ivalue);

            if (!success || !Questions.Any(q => q.Id == ivalue))
                yield return new ValidationResult(String.Format(GUI.error_multiplechoice_answer_not_element_of, ivalue));
        }
    }
}

这将在<编码> 问题通过过滤器进行的检查时,将数值[4,5]的折旧率回升。 Id 4 and 5. 如果我暂停在<代码>>><>/代码>上错开一段时间,则事后将正确进行验证。

奇怪的是,我以前曾经历过这些错误,我没有更新任何图书馆或数据库软件。

这种情况令我感到惊讶,因为似乎我无法依靠拉齐·洛达来永远工作。 或者,我是否做过错?

这与EF 4.1 装上过滤器的儿童收集器,不为许多到many工作,但我无法解释如何在此适用。

Update1: The following exception would popup by following the steps provided in the first example:

System.Data.Entity.Validation.DbEntityValidationException was unhandled by user code
  Message=Validation failed for one or more entities. See  EntityValidationErrors  property for more details.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.Internal.InternalContext.SaveChanges()
       at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
       at System.Data.Entity.DbContext.SaveChanges()
       at Caracal.Application.Controllers.SurveyController.BulkEnable(SurveyBulkAction data) in C:UsersAlessandroCaracalDigEvalProject	runkCaracalapplicationControllersSurveyController.cs:line 353
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException: 
    <really-empty>

实现这一目标的守则(不是我个人写的,而是另一个团队成员):

        bool option = data.option == "true";

        // Check if all surveys can be set to the enabled state
        foreach (int id in data.surveys)
        {
            Survey survey = Context.Surveys.SingleOrDefault(s => s.Id == id);
            if (survey == null || !survey.CanAdministrate(Context))
                return JsonResponse.Error(GUI.survey_enable_change_bulk_failed);

            surveys.Add(survey);
        }

        // Enable/disable the selected surveys.
        foreach (Survey survey in surveys)
            survey.Enabled = option;

        Context.SaveChanges();

<代码>数据是一个标有客户的后数据。 survey.CanAdministrate(Context)利用该背景阅读了亚洲开发银行组织单位的整个树木,以确定作用。

最佳回答

这是一种非常好的特征。 在某些行动中,内部转而去除酶负荷,验证就是其中之一。 这是内部实施造成这种情况的方法的一部分:

public virtual DbEntityValidationResult GetValidationResult(IDictionary<object, object> items)
{
    EntityValidator entityValidator = 
        this.InternalContext.ValidationProvider.GetEntityValidator(this);
    bool lazyLoadingEnabled = this.InternalContext.LazyLoadingEnabled;
    this.InternalContext.LazyLoadingEnabled = false;
    DbEntityValidationResult result = null;
    try
    {
        ...
    }
    finally
    {
        this.InternalContext.LazyLoadingEnabled = lazyLoadingEnabled;
    }
    return result;
}

为什么是好的? 因为它避免了当你不想要时泄露zy。 Btw. 如果你将验证逻辑推向必须装货的财产,你就错了。 你们有责任确保在验证之前填满所有必要的财产。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签