如何在反射中排除外键类型?
How to exclude foreign keys types in reflection?
我试图在遍历对象时排除外键。
我正在通过下面的代码遍历实体,但我不确定如何排除外键。
private static void Map<TEntity>(TEntity entity)
{
foreach (PropertyInfo prop in entity.GetType().GetProperties())
{
//If statement to exclude for foreign keys
Console.WriteLine(prop.Name);
}
}
这是生成的实体,
public partial class ShiftTimes
{
public ShiftTimes()
{
#region Generated Constructor
#endregion
}
#region Generated Properties
public Guid Id { get; set; }
public DateTime? DateData { get; set; }
public int? YearNo { get; set; }
public int? MonthNo { get; set; }
public int? DayNo { get; set; }
public int? StaffNo { get; set; }
public string SecondName { get; set; }
public string FirstName { get; set; }
public DateTime? TimeDataOne { get; set; }
public DateTime? TimeDataTwo { get; set; }
public string Code { get; set; }
public int? Terminal { get; set; }
public string SubSection { get; set; }
public string Shift { get; set; }
public Guid? ShiftId { get; set; }
public Guid? StaffMemberId { get; set; }
#endregion
#region Generated Relationships
public virtual ShiftCount ShiftShiftCount { get; set; }
public virtual StaffDetail StaffMemberStaffDetail { get; set; }
#endregion
}
任何正确方向的推动都将不胜感激。
因为您将从 SQL 数据库(int
、bool
、string
等)获得的所有基本数据类型都在 System
命名空间,我希望您可以过滤以仅在 System
命名空间中包含 属性 类型:
private static void Map<TEntity>(TEntity entity)
{
foreach (PropertyInfo prop in entity.GetType().GetProperties())
{
if (prop.PropertyType.FullName.StartsWith("System.")) // check if the property is in System.***
{
Console.WriteLine(prop.Name);
}
}
}
如果您的所有 Foreingkeys 都标记为虚拟,您可以在 属性 的 GetMethod 上检查该标志是否为真。
我们从 PropertyInfo 中获取 GetMethod that will give us an MehthodInfo. On the base class of that instance you'll find the IsVirtual 布尔值,如果成员标记为虚拟,则该布尔值将设置为 true。
这仍然有些脆弱,因为我希望您可以自由地将 POCO 中的其他成员标记为虚拟成员,而无需 EF 吐出。在这种情况下,您可能需要额外检查。通过检查类型所在的名称空间提供的方法可能更适合您。
这将适用于您的示例实体:
private static void Map<TEntity>(TEntity entity)
{
foreach (PropertyInfo prop in entity.GetType().GetProperties())
{
// foreignkey properties are marked Virtual
// so its GetMehod will have that bit set.
// see note 1*
if (prop.GetMethod.IsVirtual)
{
Console.WriteLine(prop.Name);
}
}
}
当我们用
执行上面的方法时
Map(new ShiftTimes());
这个输出:
ShiftShiftCount
StaffMemberStaffDetail
1. 在某些情况下,GetMethod 可能为空,但我设想的 none 会出现在特定用例中。因此,故意遗漏了对 GetMethod 的空检查。
我试图在遍历对象时排除外键。
我正在通过下面的代码遍历实体,但我不确定如何排除外键。
private static void Map<TEntity>(TEntity entity)
{
foreach (PropertyInfo prop in entity.GetType().GetProperties())
{
//If statement to exclude for foreign keys
Console.WriteLine(prop.Name);
}
}
这是生成的实体,
public partial class ShiftTimes
{
public ShiftTimes()
{
#region Generated Constructor
#endregion
}
#region Generated Properties
public Guid Id { get; set; }
public DateTime? DateData { get; set; }
public int? YearNo { get; set; }
public int? MonthNo { get; set; }
public int? DayNo { get; set; }
public int? StaffNo { get; set; }
public string SecondName { get; set; }
public string FirstName { get; set; }
public DateTime? TimeDataOne { get; set; }
public DateTime? TimeDataTwo { get; set; }
public string Code { get; set; }
public int? Terminal { get; set; }
public string SubSection { get; set; }
public string Shift { get; set; }
public Guid? ShiftId { get; set; }
public Guid? StaffMemberId { get; set; }
#endregion
#region Generated Relationships
public virtual ShiftCount ShiftShiftCount { get; set; }
public virtual StaffDetail StaffMemberStaffDetail { get; set; }
#endregion
}
任何正确方向的推动都将不胜感激。
因为您将从 SQL 数据库(int
、bool
、string
等)获得的所有基本数据类型都在 System
命名空间,我希望您可以过滤以仅在 System
命名空间中包含 属性 类型:
private static void Map<TEntity>(TEntity entity)
{
foreach (PropertyInfo prop in entity.GetType().GetProperties())
{
if (prop.PropertyType.FullName.StartsWith("System.")) // check if the property is in System.***
{
Console.WriteLine(prop.Name);
}
}
}
如果您的所有 Foreingkeys 都标记为虚拟,您可以在 属性 的 GetMethod 上检查该标志是否为真。
我们从 PropertyInfo 中获取 GetMethod that will give us an MehthodInfo. On the base class of that instance you'll find the IsVirtual 布尔值,如果成员标记为虚拟,则该布尔值将设置为 true。
这仍然有些脆弱,因为我希望您可以自由地将 POCO 中的其他成员标记为虚拟成员,而无需 EF 吐出。在这种情况下,您可能需要额外检查。通过检查类型所在的名称空间提供的方法可能更适合您。
这将适用于您的示例实体:
private static void Map<TEntity>(TEntity entity)
{
foreach (PropertyInfo prop in entity.GetType().GetProperties())
{
// foreignkey properties are marked Virtual
// so its GetMehod will have that bit set.
// see note 1*
if (prop.GetMethod.IsVirtual)
{
Console.WriteLine(prop.Name);
}
}
}
当我们用
执行上面的方法时Map(new ShiftTimes());
这个输出:
ShiftShiftCount
StaffMemberStaffDetail
1. 在某些情况下,GetMethod 可能为空,但我设想的 none 会出现在特定用例中。因此,故意遗漏了对 GetMethod 的空检查。