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:
How do I write it? Update: I would like to point out that the |
|||||||
|
Here are two methods by which you can read the meta data:
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 |
|||||||||||||||||||||||||||||||
|
It's not very pretty; reading edmx properties at runtime is not something Microsoft exposed easily or documented well (or in some cases, at all).
|

I have an auto-generated entity framework Entity Data Model Designer File (edmx). And while I am using Data Annotations for validation purposes, there are times when retrieving the maxlength of an entity property programmatically would be very useful.
In some cases, I may want to simply truncate the value, rather than throw an error if the maxlength of a property is exceeded. Or I may want to surface the maxlength property to the client for client-side validation purposes.
It turns out to be a simple query, but it took me a while to find it. You will need to reference the following libraries:
using System.Data.Objects.DataClasses;
using System.Data.Metadata.Edm;
This is the method to retrieve the MaxLength property. Please note that you may get an exception if the property does not have a MaxLength property specified.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public static int ? GetMaxLength( this EntityObject entityObject, string entityProperty) { CATDBEntities _context = new CATDBEntities(); int ? result = null ; using (_context) { var q = from meta in _context.MetadataWorkspace.GetItems(DataSpace.CSpace) .Where(m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType) from p in (meta as EntityType).Properties .Where(p => p.DeclaringType.Name == entityObject.GetType().Name && p.Name == entityProperty && p.TypeUsage.EdmType.Name == "String" ) select p; var queryResult = from meta in _context.MetadataWorkspace.GetItems(DataSpace.CSpace) .Where(m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType) from p in (meta as EntityType).Properties .Where(p => p.DeclaringType.Name == entityObject.GetType().Name && p.Name == entityProperty && p.TypeUsage.EdmType.Name == "String" ) select p.TypeUsage.Facets[ "MaxLength" ].Value; if (queryResult.Count() > 0) { result = Convert.ToInt32(queryResult.First()); } } return result; } |
To call this method, simply instantiate the entity object and pass in the proper parameters:
project _project = new project();
int? maxLength = DataAnnotation.GetMaxLength(_project, "project_name");
南来地,北往的,上班的,下岗的,走过路过不要错过!
======================个性签名=====================
之前认为Apple 的iOS 设计的要比 Android 稳定,我错了吗?
下载的许多客户端程序/游戏程序,经常会Crash,是程序写的不好(内存泄漏?刚启动也会吗?)还是iOS本身的不稳定!!!
如果在Android手机中可以简单联接到ddms,就可以查看系统log,很容易看到程序为什么出错,在iPhone中如何得知呢?试试Organizer吧,分析一下Device logs,也许有用.