private string GetPrimaryKey(ModelBase model) { Type type = model.GetType(); object[] objAtt = type.GetCustomAttributes(typeof(KeyAttribute), false); if (objAtt.Length == 0) { //如果一个实体类没有key特征,就表示这个实体类对应的表没有主键 throw new Exception("没有主键,无法执行操作"); } KeyAttribute keyAtt = objAtt[0] as KeyAttribute; return(keyAtt.KeyName); }
private string ValidateKey(ModelBase model) { Type type = model.GetType(); object[] objAtt = type.GetCustomAttributes(typeof(KeyAttribute), false); if (objAtt.Length == 0) { //如果一个实体类没有key特征,就表示这个实体类对应的表没有主键 throw new Exception("没有主键,无法执行操作"); } KeyAttribute keyAtt = objAtt[0] as KeyAttribute; string keyName = keyAtt.KeyName; if (!model.Contains(keyName)) { throw new Exception("主键没有赋值,无法执行操作"); } return(keyName); }