简体   繁体   English

在实体框架中以编程方式获取属性的“空”状态

[英]Get the 'Nullable' state programmatically of a Property in Entity Framework

I need to get the Nullable property out for a field in EF. 我需要获取EF中的字段的Nullable属性。 Some magic code needs to be done on properties that are Nullable=True, and I cannot find a working solution to get the property out. 需要对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))
   {
   }
}

Class

//    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; }
    }
}

Any help would be appreciated. 任何帮助,将不胜感激。

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. 这意味着“一些魔术代码”应该考虑到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 您可以为此使用Nullable.GetUnderlyingType方法

if (Nullable.GetUnderlyingType(propertyType) != null)
{
    // It's nullable
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架不可为空的列映射到可为空的实体属性 - Entity Framework Non-nullable column is mapped to a nullable entity property 实体框架中不可为空的导航属性为null - Non nullable navigation property is null in Entity Framework 如何检查实体框架类型的属性是否可为空 - How to check if a property of an Entity Framework type is Nullable 实体框架使双精度为可空属性 - Entity Framework making double a nullable property 以编程方式删除Entity Framework导航属性 - Programmatically removing an Entity Framework navigation property 必需的属性,但通过Code First可以实现Nullable,Entity Framework - Required property but Nullable, Entity Framework through Code First 对于可以为空的布尔属性,实体框架中的一个或多个实体的验证失败 - Validation failed for one or more entities in Entity Framework for nullable boolean property 如何禁用实体框架来创建Nullable属性 - How to disable entity framework from creating Nullable property's 如何将实体框架4.5设置为永不将任何属性设置为Nullable - How to set entity framework 4.5 to never set any property as Nullable 实体框架核心迁移构建器未在外键上设置可为空的属性 - Entity Framework Core Migrationbuilder not setting nullable property on foreign key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM