C#中如何使用DataAnnotations StringLength和子删除文本 [英] C# How to use DataAnnotations StringLength and SubString to remove text

查看:223
本文介绍了C#中如何使用DataAnnotations StringLength和子删除文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与StringLength数据注解属性的描述性和长度设置为100个字符模型类。如果此属性为超过100个字符和Entity Framework试图挽救这个属性我得到以下错误。

I have a model classes that has a description property with a data annotation attribute of StringLength and length is set to 100 characters. When this property is more than 100 characters and Entity Framework tries to save this property I get the following error.

 [StringLength(100, ErrorMessage = "Description Max Length is 100")]
        public string Description { get; set; }



错误:结果
验证失败一个或多个实体,参见EntityValidationErrors属性的更多详细信息

Error:
"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details"

我不知道,如果这有助于形成解决方案,但我使用实体框架CTP5和代码优先。

I'm not sure if this helps in forming a solution, but I'm using Entity Framework CTP5 and Code First.

我想要做什么,是,如果说明是超过100个字符,然后删除那些超过100个字符,这样的描述可以存储字符和没有错误将得到提升。

What I want to do, is if description is more than 100 characters, then remove characters that are more than 100 characters so that description can be stored and no error will be raised.

我相信我应该能够使用DataAnnotation属性StringLength手动帮我鉴定说明的有效长度,然后用子串,以消除任何在有效量的字符。

I believe I should be able to use the DataAnnotation attribute StringLength manually to help me identify the valid length of description and then use SubString to remove any characters over the valid amount.

有谁知道如何在这种情况下使用DataAnnotation?还是有另一个可用的选项?

Does anyone know how to use DataAnnotation in this situation? Or is there another options that is available?

更新
我做了什么BrokenGlass建议在这里我实现的,如果:

Update I did what BrokenGlass suggested and here my implementation if:

public static class DataAnnotation
{
    public static int? GetMaxLengthFromStringLengthAttribute(Type modelClass, string propertyName)
    {
        int? maxLength = null;
        var attribute = modelClass.GetProperties()
                        .Where(p => p.Name == propertyName)
                        .Single()
                        .GetCustomAttributes(typeof(StringLengthAttribute), true)
                        .Single() as StringLengthAttribute;

        if (attribute != null)
            maxLength = attribute.MaximumLength;

        return maxLength;
    }
}


int? maxLength = DataAnnotation.GetMaxLengthFromStringLengthAttribute(typeof(Car), "Description");

if(maxLength != null && car.Description.Length > maxLength)
    car.Description = car.Description.Substring(0, maxLength.Value);



BarDev

BarDev

推荐答案

您可以随时查询使用反射属性值,尽管这种方法是不是最好的,如果你能在它附近得到 - 它不漂亮:

You could always check the attribute value using reflection, though that approach is not the best if you can get around it - it's not pretty:

var attribute = typeof(ModelClass).GetProperties()
                                  .Where(p => p.Name == "Description")
                                  .Single()
                                  .GetCustomAttributes(typeof(StringLengthAttribute), true) 
                                  .Single() as StringLengthAttribute;

Console.WriteLine("Maximum Length: {0}", attribute.MaximumLength);    

这篇关于C#中如何使用DataAnnotations StringLength和子删除文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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