【问题标题】:How to determine which entity failed on SaveChanges如何确定哪个实体在 SaveChanges 上失败
【发布时间】:2010-08-13 05:06:33
【问题描述】:

当我调用 SaveChanges 时,我的对象上下文中有许多待处理的更改。某处有一个实体,其列的值太长。这会导致 SqlException: String or binary data will be truncated。

问题是如何确定违规实体/列?

【问题讨论】:

    标签: entity-framework-4


    【解决方案1】:

    您可以考虑使用DataAnnotations 并构建您的好友类以进行验证。然后,如果用户的数据不正确,您会向用户显示一个友好的验证错误。

    Imports System.ComponentModel.DataAnnotations 
    
    Namespace Domain 
    #Region "Validation" 
    
    <MetadataType(GetType(UserMetaData))> _ 
    Partial Public Class User 
    End Class 
    
    
    ''' <summary> 
    ''' Validation for all User data. 
    ''' </summary> 
    ''' <remarks>All validation is done at the Service Layer</remarks> 
    Public Class UserMetaData 
    
        <DisplayName("name")> _ 
        <Required(ErrorMessage:="Username is required.")> _ 
        <StringLength(30, ErrorMessage:="Username cannot exceed 30 characters.")> _ 
        <RegularExpression("^\w{3,30}$", ErrorMessage:="Not a valid username.")> _ 
        Public Property UserName As String 
    
        <DisplayName("email")> _ 
        <StringLength(50, ErrorMessage:="Email Address cannot exceed 50 characters.")> _ 
        <RegularExpression("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$", ErrorMessage:="Not a valid email address.")> _ 
        Public Property Email As String 
    
        <DisplayName("website")> _ 
        <StringLength(256, ErrorMessage:="Web Address cannot exceed 256 characters.")> _ 
        <RegularExpression("^http(s?)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$", ErrorMessage:="Not a valid website address.")> _ 
        Public Property WebSite As String 
    
        <DisplayName("about")> _ 
        <StringLength(2000, ErrorMessage:="Profile cannot exceed 2000 characters.")> _ 
        Public Property About As String 
    
        <DisplayName("region")> _ 
        <Required(ErrorMessage:="Region is required.")> _ 
        Public Property UserRegion As Integer 
    
        <DisplayName("birthdate")> _ 
        <DisplayFormat(ApplyFormatInEditMode:=True, ConvertEmptyStringToNull:=True, DataFormatString:="{0:MM/dd/yyyy}")> _ 
        Public Property BirthDate As DateTime 
    
    End Class 
    #End Region 
    End Namespace
    

    更多参考

    http://adventuresdotnet.blogspot.com/2009/08/aspnet-webforms-validation-with-data.html
    http://blogs.msdn.com/b/jimoneil/archive/2008/07/08/dynamic-data-annotations.aspx
    http://www.ipreferjim.com/site/2010/05/system-componentmodel-dataannotations-for-asp-net-web-forms/

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 你很有趣——说真的,我需要能够告诉用户,修复字段 x,因为它太长了。我可能是错的,但是像“出了点问题,由于您的数据,请启动 SQL Profiler 并自己解决”之类的消息可能不会与用户一起使用。
      • 虽然简短,但 Dave 的回答并非“错误”...您没有指定需要向用户显示错误...您确实指定了您需要找出什么出错。 SQL Profiler 会告诉你这些信息。带有 Buddy Classes 的 DataAnnotations 将帮助您为您的用户执行验证。在下面查看我的答案。
      • 确实,我误解了这是一个验证问题。应在前端进行验证以防止此错误。每个具有长度限制的属性都应进行注释,以便在值无效时进行通知。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      • 1970-01-01
      • 2011-10-17
      • 2011-12-07
      • 1970-01-01
      相关资源
      最近更新 更多