Мне нужно получить свойство Nullable для поля в EF. Некоторый магический код должен быть выполнен для свойств, которые имеют значение Nullable=True, и я не могу найти работающее решение для получения свойства.
foreach (PropertyInfo property in (PropertyInfo[])type.GetProperties())
{
var getPropertyType = property.GetMethod.ReturnTypeCustomAttributes.ToString();
var getValue = property.GetValue(model);
bool isNullable = property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>);
// isNullable always returns false !!!! and I need it to return true if the field is allowed to be null
if ((getValue == null) && (!isNullable))
{
}
}
Сорт
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace testApp.Data
{
using System;
using System.Collections.Generic;
public partial class Person
{
public int PersonId { get; set; }
public string Email { get; set; }
}
}
Любая помощь будет оценена по достоинству.
type
— будет полезно увидеть, какие свойства вы ожидаете обнулить. В настоящее время код выглядит хорошо (существующее подозрение заключается в том, что GetProperties по умолчанию будет возвращать только все общедоступные свойства, поэтому я подозреваю, что ваши свойства Nullable не являются общедоступными) - person Rich O'Kelly   schedule 09.04.2015Nullable=True
в Entity Framework не дает вамSystem.Nullable<type>
. Возможный ответ: http://stackoverflow.com/questions/18710697/how-to-check-if-a-property-of-an-entity-framework-type-is-nullable - person freedomn-m   schedule 09.04.2015