Linq to SQL - Column Lengths
Is it possible to determine the length of a column (property) from a Dlinq query? For example, I would like to set the maxlength property of a textbox to the length of a column in my database. I can't see any way to do this with the Dlinq object model. For example:
MyDatabaseContext db = new MyDatabaseContact(@"MyConnString");
var customers = From c in Customers
Select c;
Is there anyway to use the customers object to determine the fields lengths? If not, is there a way to use the databasecontext object to do it? Thanks in advance
When you create the DatabaseContext, all the DBTypes get converted into .Net types so you can't access them in a simple way. The information is stored in the DBType mapping but I don't think this gets exposed.
The only way would be to reflect on your DataContext and get the attribute information for the column or query the database (sys views) to return the information.
BUT! I have heard that Beta 2 has new features to support validation. Not sure if this will provide anything like this or if its mainly focused around Partial Methods but wait and see...
Sorry I can't be of more help.
Ben
Ben has covered the basics about the DB type being mapped to a CLR type. As Ben points out, you can get the DbType in the mapping from the attributes or mapping file but better yet, you can also get it from the APIs in a common way. It is a bit involved since it is part of all the mapping information available through MappingSource (MappingSource.GetModel(...).GetMetaType(...).GetDataMember(...).DbType). The result is a DbType string that will need to be parsed.
So currently there is no one step API to get the column lengths.
Thanks,
Dinesh