[Column(Storage= "_number" , DbType= "VarChar(50) NOT NULL" , CanBeNull= false )]
public string number
{ get { .... } set { .... } }
* This source code was highlighted with Source Code Highlighter .
This shows that we can find out from the ColumnAttribute of this property when our field can be null. Create a partial class for LinqBugTable, which will display the information about its properties:
partial class LinqBugTable
{
public void WriteNullableInfo()
{
foreach (PropertyInfo property in GetType().GetProperties())
{
foreach (ColumnAttribute columnAttribute in property.GetCustomAttributes( typeof (ColumnAttribute), true ))
{
Console .WriteLine( "Property: '{0}', CanBeNull: '{1}'" , property.Name, columnAttribute.CanBeNull);
}
}
}
}
* This source code was highlighted with Source Code Highlighter .
It seems everything is ready, but the result will be as follows:
[Column(Storage= "_price" , DbType= "Money NOT NULL" )]
public decimal price
{ get { .... } set { .... } }
* This source code was highlighted with Source Code Highlighter .
As you can see, the CanBeNull property of the Column attribute is not set, but, in general, bool variables are set to false, so maybe this is not a problem (I thought at first). It is good that there is a wonderful Reflector program with which you can see what happens in the ColumnAttribute code:
public void WriteNullableInfo()
{
foreach (PropertyInfo property in GetType().GetProperties())
{
foreach (ColumnAttribute columnAttribute in property.GetCustomAttributes( typeof (ColumnAttribute), true ))
{
bool canBeNull = (Nullable.GetUnderlyingType(property.PropertyType) != null )
|| (columnAttribute.CanBeNull && !property.PropertyType.IsValueType);
Console .WriteLine( "Property: '{0}', CanBeNull: '{1}'" , property.Name, canBeNull);
}
}
}
* This source code was highlighted with Source Code Highlighter .
Now we check that if we have a property of type Nullable <T>, then we will return True, otherwise we will return the value CanBeNull, taking into account that the field type is not a struct. Well, we hope that with the property types of ValueType - this is the only trouble that was in this scheme. ;)
Source: https://habr.com/ru/post/58119/