如何使用create query方法在实体框架中动态创建插入查询?

时间:2009-04-20 16:15:34

标签: entity-framework entity

我正在使用类似上面示例代码的东西,但是当我尝试执行它时,它会说 查询语法错误,查询语法中的关键字附近有错误...

System.Data.Objects.ObjectParameter[] opc=new System.Data.Objects.ObjectParameter[1];  //sample
opc[0]=new System.Data.Objects.ObjectParameter("columnname","columnvaluetoinsert")
string strQuery="insert into tablename(columnname)values(@columnname)";
var query1 = entities.CreateQuery<System.Data.Objects.DataClasses.EntityObject>(strQuery, opc);
query1.Execute(System.Data.Objects.MergeOption.NoTracking);

1 个答案:

答案 0 :(得分:0)

现在经过四个多月左右,我可以回答这个......

string tableName = "yourTableName";
string PrimaryColName = "PrimarykeyName";

using(Entities entities = new Entities) 
{ 
  string sQuery= " select value " + tableName + " from " + tableName + " as " + tableName; 
 sQuery += " where ";
 sQuery += tableName + ".PrimaryColName " = " + lngMasterId;

 System.Data.Objects.ObjectQuery query = new System.Data.Objects.ObjectQuery(sMasterTableName, entities); 
 //where entities is the object of your objectContext class :) 
 return query.First();//do whatever you want with the result 
}
"value"告知以自定义匿名格式提供结果后,

"select"