可用于EF获取实体字段是否为主键key
引用空间:
System.Reflection.PropertyInfo
AnUser anUser = new AnUser();anUser.Id = "514f275979f64531b7fbbb2f89c8af49";anUser.UserNo = "5566110";PropertyInfo[] props = typeof(AnUser).GetProperties();//实体的字段列表foreach (var item in props){//item.Name 获取字段名称if (item.Name == "Id"){string id = item.GetValue(anUser) as string;//获取字段值}var kkkk = item.Attributes;var kkkk2 = item.CustomAttributes;//自定义的属性标签//获取字段是否有[Key]属性标签bool isHave = kkkk2.Any(x => x.AttributeType == typeof(ponentModel.DataAnnotations.KeyAttribute));}
using System;using System.Collections.Generic;using ponentModel.DataAnnotations.Schema;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Ayy.Models{/// <summary>/// 用户/// </summary>[Table("AnUser")]public class AnUser{/// <summary>/// guid主键/// </summary>[Key]public string Id {set; get; }/// <summary>/// 工号/// </summary>public string UserNo {get; set; }}}