hea*_*avy 7 c# asp.net entity-framework c#-4.0
我有一个表单,它有一些字段,并与数据库有关系.我正在使用实体框架我想在sql server发送错误消息之前处理异常.例如,当用户在sql server处理它之前在数字字段win或web应用程序处理异常中输入字符串值时.我编写了这段代码,但它并不适用于所有异常.例如,如果字段为空或具有无效类型,则表示输入字符串的格式不正确.
using (var context = new entityTestEntities2())
{
try
{
int stNumber = Convert.ToInt32(textBox2.Text);
var allCustomers = context.setcust(null, stNumber);
}
catch(Exception ex)
{
if (ex.Message.Contains("correct format"))
{
int x= System.Runtime.InteropServices.Marshal.GetExceptionCode();
MessageBox.Show("error number"+x.ToString()+ex.Message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
您应该首先在UI上进行验证,然后处理与Entity Framework相关的特定错误。
创建一个模型并使用数据注释:
using System.ComponentModel.DataAnnotations;
public class YourViewModel
{
[Required]
[Range(0, 15, ErrorMessage = "Can only be between 0 .. 15")]
public int stNumber { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在控制器中,将模型返回到视图:
var model = new YourViewModel();
return View(model);
Run Code Online (Sandbox Code Playgroud)
通过将模型添加到视图并使用一些标签助手将文本框绑定到模型:
@using YourProject.WebUI.Models
@model YourViewModel
@Html.TextBoxFor(m => m.stNumber )
@Html.ValidationMessageFor(m => m.stNumber )
Run Code Online (Sandbox Code Playgroud)
现在,当有人尝试输入非数字或超出范围的数字时,在将错误数据发送回控制器之前,将向用户显示错误。
要处理实体框架异常,请使用try catch:
try
{
var entity = context.yourEntity.FirstOrDefault(o => o.Id == custId);
if (entity == null) return false;
entity.value= stNumber;
entity.ModifiedBy = userId;
entity.ModifiedDate = DateTime.Now;
Db.SaveChanges();
return true;
}
catch (DbUpdateException Ex)
{
Console.WriteLine(ex.InnerException.Message);
return false;
}
Run Code Online (Sandbox Code Playgroud)
其他异常类型包括:
DbUpdateException
Run Code Online (Sandbox Code Playgroud)
向数据库发送更新时发生错误。
DbUpdateConcurrencyException
Run Code Online (Sandbox Code Playgroud)
数据库命令不会影响预期的行数。这通常表示乐观的并发冲突;也就是说,自查询以来,数据库中的行已更改。
DbEntityValidationException
Run Code Online (Sandbox Code Playgroud)
保存失败,因为对实体属性值的验证失败。
NotSupportedException
Run Code Online (Sandbox Code Playgroud)
尝试使用不受支持的行为,例如在同一上下文实例上同时执行多个异步命令。
ObjectDisposedException
Run Code Online (Sandbox Code Playgroud)
上下文或连接已处置。
InvalidOperationException
Run Code Online (Sandbox Code Playgroud)
在向数据库发送命令之前或之后,尝试在上下文中处理实体时发生了一些错误。
您应该捕获 SqlException,而不是捕获 Exception。
SqlException 有一个可以使用的 number 属性:
catch (SqlException e)
{
MessageBox.Show("Error number: "+e.Number + " - " + e.Message);
}
Run Code Online (Sandbox Code Playgroud)
您应该做的是找到适合您的解决方案模型的架构。一般来说,我会在创建上下文之前进行验证。如果您的应用程序需要更多验证,您可能需要为此目的创建一个验证层。
public class RuleViolation
{
public string Property {get; set;}
public string Message {get; set;}
}
public class Program
{
public static List<RuleViolation> GetRuleViolations(string[] parameters)
{
List<RuleViolation> validations = new List<RuleViolation>();
if(!int.TryParse(parameters[0], out new Int32()))
{
validations.Add(new RuleViolation{Message ="Input1 must be integer.", Property = "input1"});
}
//more validation
return validations;
}
public static void Main(string[] parameters)
{
var validations = GetRuleViolations(parameters);
if(validations.Any())
{
validations.ForEach(x=> Console.WriteLine(x.Message));
return;
}
int input1 = int.Parse(parameters[0]);
//after all your business logic are ok, then you can go to persistence layer to hit the database.
using (var context = new entityTestEntities2())
{
try
{
var allCustomers = context.setcust(null, input1);
}
catch(SqlException exc)
{
//here you might still get some exceptions but not about validation.
ExceptionManager.Log(exc);
//sometimes you may want to throw the exception to upper layers for handle it better over there!
throw;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望这个例子能让验证逻辑的架构更加清晰。
| 归档时间: |
|
| 查看次数: |
33621 次 |
| 最近记录: |