MVC实体框架验证,以prevent重复记录 [英] MVC Entity Framework validation to prevent duplicate records

查看:117
本文介绍了MVC实体框架验证,以prevent重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是新来的MVC和EF所以这可能是一个非常简单的问题,但什么是prevent最好的方式,从试图进入重复记录用户?

Hi i'm new to MVC and EF so this may be a really simple question but what is the best way to prevent the user from trying to enter duplicate records?

我有一列是主键一个简单的查找表。我创建一个维护屏幕使管理员可以添加额外的项目到查找列表。我的控制器是这样的:

I have a simple look up table with one column which is the primary key. I'm creating a maintenance screen so admins can add extra items to the look up list. My controller looks like :

public ActionResult Index(string NewRow)
    {
        try
        {
            CaseType t = new CaseType { ID = NewRow };
            if (ModelState.IsValid)
            {
                UOW.CaseTypes.Add(t);
                UOW.Save();
            }
        }
        catch (Exception ex)
        {
            ModelState.AddModelError("", ex.Message);
        }
        return View(UOW.CaseTypes.All());
    }

停靠重复的记录,并停止死亡的黄色屏幕,但显示的信息是:发生错误,同时更新条目详细信息请参阅内部异常。这对用户没有好处。

Which stops the duplicate records and stops the yellow screen of death but the message displayed is : "An error occurred while updating the entries. See the inner exception for details." which is no good for the users.

我只是想知道是否有更好的方法来捕获错误。

I was just wondering if there is a better way to catch the error.

推荐答案

有关显示验证错误我用的是这样的:

For show validation error I use something like this:

MainEmail它从视图模型属性。

MainEmail it's property from ViewModel

        var mod = ModelState.First(c => c.Key == "MainEmail");  // this
        mod.Value.Errors.Add("Row's shouldn't duplicates.");    // this

        if (ModelState.IsValid)
        {
            return RedirectToAction("Details");
        }

        return View(client);

错误将显示在这一领域的考虑:

Error will show's in this field in view:

    <div class="editor-label">
        @Html.LabelFor(model => model.MainEmail)
    </div>

和对未来,就必须隐藏自己的错误屏幕!你需要显示自定义错误页:

And for future, you must hide your error screen! You need to display a custom error page:

如果你用asp-MVC-3,添加到web.config中这样的字符串:

If you use asp-mvc-3, add to web.config such string:

  <system.web>
     <customErrors mode="On" defaultRedirect="~/Error" />
     ...

和用户将有/Shared/Error.cshtml页面异常消息insted的页面(可显示sequrity数据)。

And users will have /Shared/Error.cshtml page insted of page with exception message (which can show sequrity data).

添加

关于唯一约束创建了这里讨论
<一href=\"http://stackoverflow.com/questions/4413084/unique-constraint-in-entity-framework-$c$c-first\">Unique约束在实体框架code首先

About unique constraint creation was discussed here Unique Constraint in Entity Framework Code First

比可以查看重复记录你试试code。

than you can check about records duplication with your try code.

在我的应用程序首先使用code和,因为它导致低可测性不添加唯一约束。在这种情况下,用简单的查找的保存更改。

In my application I use code first and don't add unique constraint because it's lead to low testability. In that case use simple Find before saving changes.

首先是形式给出得快一点。

First aproach is little bit faster.

办法二是长大了可测试性。

Approach two is grow up testability.

这篇关于MVC实体框架验证,以prevent重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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