EF의 필드에 대해 Nullable 속성을 가져와야합니다. 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; }
}
}
어떤 도움을 주시면 감사하겠습니다.
따라서 데이터베이스에서 nullable 속성을 원합니다. 이는 "일부 매직 코드"가 EDMX의 스토리지 모델을 조사해야 함을 의미합니다. 클래스 모델에는 생성 된 클래스는 물론이 정보가 포함되어 있지 않습니다.
스토리지 모델의 모든 nullable 속성 목록을 가져 오는 방법은 다음과 같습니다.
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));
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다