I need to get the Nullable property out for a field in EF. Some magic code needs to be done on properties that are Nullable=True, and I cannot find a working solution to get the
So you want the properties that are nullable in the database. That means that "some magic code" should look into the storage model of the EDMX. The class model doesn't contain this information, let alone the generated classes.
Here's how you can get a listing of all nullable properties in the storage model:
var tableName = "someTable";
var oc = ((IObjectContextAdapter)context).ObjectContext;
var items = oc.MetadataWorkspace.GetItems(DataSpace.SSpace).OfType<EntityType>();
foreach (var entityType in items.Where(e => e.Name == tableName))
{
var props = string.Join(",", entityType.Properties.Where(p => p.Nullable));
Debug.WriteLine(string.Format("{0}: {1}", entityType.Name, props));
}
You can use Nullable.GetUnderlyingType Method for this
if (Nullable.GetUnderlyingType(propertyType) != null)
{
// It's nullable
}