动态数据| LINQ TO SQL |一般验证

发布于 2024-08-09 05:48:29 字数 724 浏览 8 评论 0原文

我有 5 个不同的实体,为其生成了动态数据(使用 LINQTOSQL)。 在任何这些实体的插入(Insert.aspx)上,如果出现错误,我想通知用户发生了错误,并可能显示一些通用错误消息。

1)我不是在谈论常规的必填字段错误,而是像“违反唯一约束”之类的内容

2)我可以通过执行以下操作分别为每个页面执行此操作:

protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e) {
    if (e.Exception == null || e.ExceptionHandled)
    {
        Response.Redirect(table.ListActionPath);
    }
    else
    {
        //OtherErrors is the label on the page
        OtherErrors.Visible = true;
        OtherErrors.Text = e.Exception.Message;
        OtherErrors.DataBind();
        e.ExceptionHandled = true;
        e.KeepInInsertMode = true;

    }
}

3)但是,我想创建一些非常通用的内容将适用于所有实体的所有插入

I have 5 different entities for which dynamic data(with LINQTOSQL) was generated.
On Insert(Insert.aspx) of any of these entities, if there is an error, I would like to notify user that error happened and possibly show some generic error message.

1) I am not talking about regular required field errors but something like "Unique constraint violation"

2) I can do it for each page separately by doing something like this:

protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e) {
    if (e.Exception == null || e.ExceptionHandled)
    {
        Response.Redirect(table.ListActionPath);
    }
    else
    {
        //OtherErrors is the label on the page
        OtherErrors.Visible = true;
        OtherErrors.Text = e.Exception.Message;
        OtherErrors.DataBind();
        e.ExceptionHandled = true;
        e.KeepInInsertMode = true;

    }
}

3) BUT, I want to created something very generic that will work for all inserts across all entities

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酷遇一生 2024-08-16 05:48:29

您可以通过在 ADO.NET 实体框架类中创建事件处理程序来自定义验证:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.DynamicData;
using System;
using System.Data;
using System.Data.Objects;

namespace AdventureWorksLTModel 
{
    public partial class AdventureWorksLTEntities 
    {
        partial void OnContextCreated() 
        {
            this.SavingChanges += new System.EventHandler(OnSavingChanges);
        }

        public void OnSavingChanges(object sender, System.EventArgs e) 
        {
            var stateManager = ((AdventureWorksLTEntities)sender).ObjectStateManager;
            var changedEntities = ObjectStateManager.GetObjectStateEntries (EntityState.Modified | EntityState.Added);

            // validation check logic
            throw new ValidationException("Something went wrong.");
        }

    }
}

DynamicValidator 控件将捕获数据模型中引发的任何验证异常。动态数据项目中包含的页面模板包含 DynamicValidator 控件,该控件在页面上显示验证错误。

You can customize validation by creating an event handler in the ADO.NET Entity Framework class:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.DynamicData;
using System;
using System.Data;
using System.Data.Objects;

namespace AdventureWorksLTModel 
{
    public partial class AdventureWorksLTEntities 
    {
        partial void OnContextCreated() 
        {
            this.SavingChanges += new System.EventHandler(OnSavingChanges);
        }

        public void OnSavingChanges(object sender, System.EventArgs e) 
        {
            var stateManager = ((AdventureWorksLTEntities)sender).ObjectStateManager;
            var changedEntities = ObjectStateManager.GetObjectStateEntries (EntityState.Modified | EntityState.Added);

            // validation check logic
            throw new ValidationException("Something went wrong.");
        }

    }
}

Any validation exceptions that are thrown in the data model are caught by the DynamicValidator control. The page templates included with a Dynamic Data project contain a DynamicValidator control, which displays the validations errors on the page.

乞讨 2024-08-16 05:48:29

我无法针对您的情况完全测试这一点,但是您可以重写 SubmitChanges 方法。

 public partial class MyNorthwindDataContext : NorthwindDataContext
 {

  public MyNorthwindDataContext()
  {

  }

  public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode)
  {
   //catch error logic here...

   base.SubmitChanges(failureMode);
  }
 }

I can't fully test this for your situation but, you could override the SubmitChanges method.

 public partial class MyNorthwindDataContext : NorthwindDataContext
 {

  public MyNorthwindDataContext()
  {

  }

  public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode)
  {
   //catch error logic here...

   base.SubmitChanges(failureMode);
  }
 }
暖阳 2024-08-16 05:48:29
    public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode)
{

    try
    {
        base.SubmitChanges(failureMode);
    }
    catch (Exception e)
    {
        throw new ValidationException("Something is wrong", e);
    }

}
    public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode)
{

    try
    {
        base.SubmitChanges(failureMode);
    }
    catch (Exception e)
    {
        throw new ValidationException("Something is wrong", e);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文