儿童约束实体的实体框架的数量 [英] Constrain the number of child entities in Entity Framework

查看:122
本文介绍了儿童约束实体的实体框架的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法,我可以约束,可以属于实体框架父子实体的数量简洁的方式。我使用的4.3.1的时刻。

Is there a succinct way that I can constrain the number of child entities that can belong to a parent in Entity Framework. I am using 4.3.1 at the moment.

我正在开发通过使用Entity Framework的数据访问层访问数据的ASP.NET MVC3网站。我有其中有一个多对多的关系到搜索实体SearchList实体。一个SearchList可能有很多搜索,并且搜索可能属于很多SearchLists。

I am developing an ASP.NET MVC3 site which accesses data via a data access layer that uses Entity Framework. I have a SearchList entity which has a many to many relationship to a Search entity. A SearchList may have many Searches, and a Search may belong to many SearchLists.

目前在现场的工作流一点,用户需要选择搜索和其他物品的批量查找来使用。我们希望页面加载整个搜索列表。

At one point in the workflow of the site, a user needs to select the searches and other items to use in a batch search. We want the page to load the entire search list.

SearchLists可以得到相当大的,作为一个测试,我们创建了一个21 000搜索。它花了几秒钟,返回的数据约为9.5 MB,而我们期待的,但试图在jQueryUI的哽咽表IFY多。

SearchLists can get quite large, and as a test we created one with 21,000 searches. It took a few seconds, and the data returned was about 9.5 MB, which we were expecting, but jQueryUI choked when trying to table-ify that much.

因此​​,我们要强加给任何搜索搜索列表可以有数量限制。我可以通过应用程序,并把一堆规则来检查集合的大小,如果要添加正试图搜查加上当前...亚达内容十分重要的大小。

So we want to impose a limit on the number of searches any search list can have. I can go through the application and put a bunch of rules in that checks the size of the collection and if the searches that are trying to be added plus the size of the current... yada yada yada.

如果但有一个更好的办法(尤其是一个可以轻松地输出MVC便拿起一个错误消息)我将完全采取替代。

If however there was a better way (especially one that could easily output an error message that MVC would pick up) I would totally take that instead.

我用Google搜索,并仔细研读了许多EF博客无济于事。约束孩子​​,在收集和类似的搜索儿童的最大数目已经返回的结果是对LINQ查询和伯爵和最大的方法。

I have googled, and perused a number of EF blogs to no avail. Constrain children and max number of children in collection and similar searches have returned results that are about Linq queries and the Count and Max methods.

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

我结束了CustomValidationAttribute去,并成功的大量付诸实施。请参阅下面我实现信息:

I ended up going with CustomValidationAttribute, and implemented it with a great deal of success. See below for my implementation info:

    [NotMapped]
    public String ValidationMessage { get; set; }
    [CustomValidation(typeof(EntityValidation.EntityValidators), "ValidateSearchCount")]
    public virtual List<Search> Searches { get; set; }

    public static bool Create(ProjectContext db, SearchList searchList)
    {
        try
        {
            db.SearchLists.Add(searchList);
            db.SaveChanges();
            return true;
        }
        catch (DbEntityValidationException dbEx)
        {
            foreach (var validationErrors in dbEx.EntityValidationErrors)
            {
                foreach (var validationError in validationErrors.ValidationErrors)
                {
                    searchList.ValidationMessage += validationError.ErrorMessage;
                }
            }
            return false;
        }
        catch (Exception)
        {
            return false;
        }
    }

EntityValidators类

    public static ValidationResult ValidateSearchCount(List<Search> Searches)
    {
        bool isValid;
        int count = Searches.Count();
        isValid = (count <= 5000 ? true : false);

        if (isValid)
        {
            return ValidationResult.Success;
        }
        else
        {
            return new ValidationResult("A maximum of 5000 searches may be added to a SearchList.");
        }
    }

一个类似的异常块是关于更新方法。这样,当调用SaveChanges被调用它试图验证实体及其子集,当收集计数大于5000的验证器返回比夹在异常处理程序,并存储在本地属性的错误消息我控制器检查出问题时。

A similar exception block is on the update method. In this way, when SaveChanges gets called it attempts to validate the entity and its child collections, and when the collection count is greater than 5000 the validator will return an error message which gets caught in the exception handler and stored in a local property for my controller to check when things go wrong.

这篇关于儿童约束实体的实体框架的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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