linq to sql - LINQ2SQL: How to implement a generic maximum string length validation? -
LINQ2 is a common problem in SQL, whereas while the .NET string allows any length to specify its variable, your database may have a specific maximum length interval (such as VARCHAR (5)). This will give the SQL error message "string or binary data will be deducted", which is extremely useless because it tells you which areas are criminals.
Obviously, the maximum string length will be the right way. The main problem that I encountered, is making the required verification for every linky object in my project, and updating the verification if the maximum length of the field is updated.
Ideally, I should work dynamically in a way to determine the maximum length of a generated field, so I do not risk risk updating later.
I can find the best possible implementation so far, which is better than ever, which I can think of. Only fixed-point dynamic maximum length is set.
Has anyone seen or implemented anything like that?
The LINQ2SQL code generator retains a feature on the property field:
It will be easy to remove and use this information at runtime:
The public class line {// is usually generated by LINQ2SQL [column (storage = "_Message", DBTIP = "Navkaraka (20)")]; Public string message {get; Set; } // Generally generated by LINQ2SQL [column (storage = "_Property", dbipp = "nerve (20) no tap") {acquisition of public string property; Set; }} Public class VarCharInfo {public int? MaxLen; Public boolean isoble; } Public static VarCharInfo GetVarCharInfo (PropertyInfo propertyInfo) {var attrib = propertyInfo.GetCustomAttributes (incorrectly), typeof (ColumnAttribute) .OfType & lt; ColumnAttribute & gt; () .FirstOrDefault (); If (attrib == tap; attrib.DbType == faucet) {return tap; } Var match = Regex.Match (attrib.DbType, @ "VarChar \ ((? 'Len' \ d +) \) (? 'Notnull' tap)?"); If (match! Success) {return tap; } Var rvl = new VarCharInfo (); Rvl.maxlen = int.Parse (match group ["lane"]. Capture [0]. Value); Rvl.IsNullable = match.Groups ["notnull"]. Success; Return RGL; } Public Stable Bool Valid ValidColumns (Object Data Object) {bool success = true; Foreach (var property statistics dataObject.GetType () .GetProperties (). Where (P => pi.PropertyType == typeof (string))) {var vci = GetVarCharInfo (propertyInfo); If (vci! = Null) {var currentValue = propertyInfo.GetGetMethod (). In the form of a string (Data object, empty) if (current value == tap) {if (! Vci.IsNullable) {// more work: value is zero, but this should not be success = false; }} And if (vci.MaxLen! = Null & amp; amp;; CurrentValue.Length & gt; vci.MaxLen) {// more work: value is greater than DB = success; }}} Success of Return; } Static zero usage test () {rows = new line (); T.Message = "This message exceeds 20 characters"; // t Property Vacant Valuation Columns (T); // false returns! }