C#-检查模型上的属性是否为Key



我目前正在为我现在的雇主创建我自己的ORM,因为他们在nuget上的包有限制,在我的项目上安装它们需要批准,他们需要数周或数月的时间才能批准(从下一级到最高级),这就是为什么我要创建自己的ORM,但并不是我们数据库中的所有表都有不同名称的Id或主键(遗留应用程序),这就是我计划使其灵活的原因。

假设我有一个带有主键的模型

public class Sample
{
[Key]
public int Id { get; set; }
[Required]
[StringLength(50)]
public string FirstName { get; set; }
[Required]
[StringLength(50)]
public string LastName { get; set; }
[Required]
public int Age { get; set; }
[StringLength(255)]
public string Hobby { get; set; }
}

然后我创建了一个将数据添加到数据库表的insert方法,现在如果主键的名称仅为";Id";,但在具有不同名称的遗留数据库上,如下面

[Key]
public int EntryNo { get; set; }

或者这个

[Key]
public string TransNo { get; set; }

我希望插入中的代码确定该属性是否是主键,我的代码现在正在做的就是这样。

public T InsertData<T>(T model)
{
using (SqlConnection SqlConn = new SqlConnection(_connectionString))
{
T GetResult;
Type t = typeof(T);
Type ObjType = model.GetType();

string SqlParameter = "";
string SqlValue = "";
PropertyInfo[] prop = ObjType.GetProperties();
for(int i = 0; i < prop.Length; i++)
{
if (prop[i].Name != "Id") //I want this to be replace to check if the property is a primary key
{
SqlParameter = (SqlParameter == "") ? $"[{prop[i].Name}]" : $"{SqlParameter},[{prop[i].Name}]";
SqlValue = (SqlValue == "") ? $"'{prop[i].GetValue(model).ToString()}'" : $"{SqlValue},'{prop[i].GetValue(model).ToString()}'";
}
}
string SqlString = $"insert into [{ObjType.Name}]({SqlParameter})values({SqlValue})";
SqlCommand SqlCmd = new SqlCommand(SqlString, SqlConn);
SqlConn.Open();
SqlCmd.ExecuteNonQuery();
SqlConn.Close();
//Get Inserted data
GetResult = GetInsertData(model);
return GetResult;
}
}

现在我想更改if语句来检查该属性是否是主键,这样它就不会坚持";Id";它还可以用于我们的遗留应用程序。

目前我正在互联网上搜索,但我看不到任何内容。

您可以查找DataAnnotations KeyAttribute。

public T InsertData<T>(T model)
{
using (SqlConnection SqlConn = new SqlConnection(_connectionString))
{
T GetResult;
Type t = typeof(T);
Type ObjType = model.GetType();
string SqlParameter = "";
string SqlValue = "";
PropertyInfo[] props = ObjType.GetProperties();
PropertyInfo prop = null;
for (int i = 0; i < props.Length; i++)
{
if (props[i].Name == "Id") //I want this to be replace to check if the property is a primary key
{
prop = props[i];
break;
}
object[] attrs = props[i].GetCustomAttributes(typeof(KeyAttribute), false);
if (attrs.Length > 0)
{
prop = props[i];
break;
}
}
if (prop == null)
{
throw new Exception("pk not found");
}
SqlParameter = (SqlParameter == "") ? $"[{prop.Name}]" : $"{SqlParameter},[{prop.Name}]";
SqlValue = (SqlValue == "") ? $"'{prop.GetValue(model).ToString()}'" : $"{SqlValue},'{prop.GetValue(model).ToString()}'";

string SqlString = $"insert into [{ObjType.Name}]({SqlParameter})values({SqlValue})";
SqlCommand SqlCmd = new SqlCommand(SqlString, SqlConn);
SqlConn.Open();
SqlCmd.ExecuteNonQuery();
SqlConn.Close();
//Get Inserted data
GetResult = GetInsertData(model);
return GetResult;
}
}

看看下面的问题的解决方案,你的问题的前提是一样的。

类似问题

Nic

谢谢你,我已经修改了你的代码,现在它正在工作,非常感谢,下面是我更新的代码:

public T InsertData<T>(T model)
{
using (SqlConnection SqlConn = new SqlConnection(_connectionString))
{
T GetResult;
Type t = typeof(T);
Type ObjType = model.GetType();

string SqlParameter = "";
string SqlValue = "";
PropertyInfo[] prop = ObjType.GetProperties();
for(int i = 0; i < prop.Length; i++)
{
//Check if the property is a primay key
object[] attrs = prop[i].GetCustomAttributes(typeof(KeyAttribute), false);
if(attrs.Length == 0)
{
SqlParameter = (SqlParameter == "") ? $"[{prop[i].Name}]" : $"{SqlParameter},[{prop[i].Name}]";
SqlValue = (SqlValue == "") ? $"'{prop[i].GetValue(model).ToString()}'" : $"{SqlValue},'{prop[i].GetValue(model).ToString()}'";
}
}
string SqlString = $"insert into [{ObjType.Name}]({SqlParameter})values({SqlValue})";
SqlCommand SqlCmd = new SqlCommand(SqlString, SqlConn);
SqlConn.Open();
SqlCmd.ExecuteNonQuery();
SqlConn.Close();
//Get Inserted data
GetResult = GetInsertData(model);
return GetResult;
}
}

最新更新