如何从代码中获取EDMX模型中字符串的最大长度? [英] How to get the maximum length of a string from an EDMX model in code?

查看:153
本文介绍了如何从代码中获取EDMX模型中字符串的最大长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要从用户获取输入并将其保存到一行数据库表。问题是我需要将输入字符串的长度限制在数据库中对应的VARCHAR列的宽度。



当浏览模型时,我可以清楚地看到在属性窗口中,模型知道字符串的最大长度,但我不知道如何在代码中访问此数据。



如果我想写一些东西像这样:

  Entities entities = new Entities(); 
myTable = entities.myTable.First();
if(userInput.length> myTable.columnA.MaxLength)
{
//告诉用户输入太长。
}
else
{
myTable.columnA = userInput;
}

如何写?



更新:我想指出以下答案中提到的 IObjectContextAdapater System.Data.Entity.Infrastructure 命名空间

解决方案

这里有两种方法可以阅读元数据:

  int? GetMaxLength(DbContext context,string tableName,string propertyName)
{
var oc =((IObjectContextAdapter)context).ObjectContext;

返回oc.MetadataWorkspace.GetItems(DataSpace.CSpace).OfType< EntityType>()
.Where(et =η et.Name == tableName)
.SelectMany (et = et .Properties.Where(p => p.Name == propertyName))
.Select(p => p.MaxLength)
.FirstOrDefault();
}

int? GetMaxLength< T(DbContext context,Expression< Func< T,object>> property)
{
var memberExpression =(MemberExpression)property.Body;
string propertyName = memberExpression.Member.Name;
返回GetMaxLength(context,typeof(T).Name,propertyName);
}

所以你可以输入表名和属性名称,指定您感兴趣的属性。



另一种方法可能是创建MetaData类,并使用 MaxLength 属性。


I've created an EDMX object from a database I'm programming against.

I need to get input from a user and save it to a row in the database table. The problem is that I need to limit the length of input strings to the width of the corresponding VARCHAR column in the database.

When I browse the model, I can clearly see in the properties window that the model knows the max length of the string, but I don't know how to access this data in code.

If I want to write something like this:

Entities entities = new Entities();
myTable = entities.myTable.First();
if (userInput.length > myTable.columnA.MaxLength)
{
    // tell the user that the input is too long.
}
else
{
    myTable.columnA = userInput;
}

How do I write it?

Update: I would like to point out that the IObjectContextAdapater mentioned in the answers below is in the System.Data.Entity.Infrastructure namespace.

解决方案

Here are two methods by which you can read the meta data:

int? GetMaxLength(DbContext context, string tableName, string propertyName)
{
    var oc = ((IObjectContextAdapter)context).ObjectContext;

    return oc.MetadataWorkspace.GetItems(DataSpace.CSpace).OfType<EntityType>()
             .Where(et => et.Name == tableName)
             .SelectMany(et => et.Properties.Where(p => p.Name == propertyName))
             .Select (p => p.MaxLength)
             .FirstOrDefault();
}

int? GetMaxLength<T>(DbContext context, Expression<Func<T, object>> property)
{
    var memberExpression = (MemberExpression)property.Body;
    string propertyName = memberExpression.Member.Name;
    return GetMaxLength(context, typeof(T).Name, propertyName);
}

So you can either enter the table name and property name, or an expression that specifies the property you're interested in.

Another approach could be to create a MetaData class and use the MaxLength attribute.

这篇关于如何从代码中获取EDMX模型中字符串的最大长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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