I am porting an application running on LINQ-to-SQL to the Entity Framework and it is difficult for me to find the equivalent of ExecuteCommand :
db.ExecuteCommand("INSERT Infos (Title) VALUES ('this is an added title')");
I found this site that tells me that you can implement ExecuteCommand as an extension method on your ObjectContext, leaving the existing code unchanged, adding this code to the static method in your project:
public static int ExecuteCommand(this ObjectContext objectContext,
string command) {
DbConnection connection = ((EntityConnection)objectContext.Connection).StoreConnection;
bool opening = (connection.State == ConnectionState.Closed);
if (opening)
connection.Open();
DbCommand cmd = connection.CreateCommand();
cmd.CommandText = command;
cmd.CommandType = CommandType.StoredProcedure;
try {
return cmd.ExecuteNonQuery();
}
finally {
if (opening && connection.State == ConnectionState.Open)
connection.Close();
}
}
But I tried to put it (1) in the class in which I use it, (2) in the file of the generated Entity object (3) in a static class in the project, but always get various errors. Where do I need to set this method exactly?
:
ExecuteCommand Entity Framework
ExecuteCommand() Entity Framework? Entity Framework, Visual Studio 2008 SP1.