我试图在遍历对象时排除外键。
我正在通过下面的代码遍历该实体,但是不确定如何排除外键。
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数据库获得的所有基本数据类型都在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);
}
}
}