Suppose I have a table with the column Description, varchar(100). If try to insert a string with more than 100 characters, the insert will fail.
Is there a way in En
I used a slightly different tack, but also utilizing the On*Changed methods. I'm generating partial classes using a stripped down version of the .tt file used by EF. The relevant section is where the properties are generated. The Maximum length is available and can be used to truncate the string.
foreach (EdmProperty property in
entity.Properties.Where(p => p.DeclaringType == entity
&& p.TypeUsage.EdmType is PrimitiveType))
{
/// If this is a string implements its OnChanged method
if (property.TypeUsage.ToString() != "Edm.String") continue;
int maxLength = 0;
if (property.TypeUsage.Facets["MaxLength"].Value == null) continue;
if (!Int32.TryParse(property.TypeUsage.Facets["MaxLength"].Value.ToString(),
out maxLength)) continue;
if (maxLength == 0) continue;
// Implement the On*Changed method
#>
partial void On<#= property.Name#>Changed() {
<#=code.FieldName(property)#> =#=code.FieldName(property)#>.Substring(0,<#= maxLength #>);
}
<#
}