I need to execute a SQL stored procedure every time before I request my ObjectContext. What I want to achieve is to set the value CONTEXT_INFOto a value that will be used later in most of my queries.
Has anyone done this? Is it possible?
[EDIT]
I currently achieve this by opening a connection and executing a stored procedure in my ObjectContext constructor as follows:
public partial class MyEntitiesContext
{
public MyEntitiesContext(int contextInfo) : this()
{
if (Connection.State != ConnectionState.Open)
{
Connection.Open();
}
var connection = ((EntityConnection)Connection).StoreConnection;
using (var cmd = connection.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "[dbo].[SetContextInfo]";
cmd.Parameters.Add(new SqlParameter("@ci", _contextInfo));
cmd.ExecuteNonQuery();
}
}
}
Then in my integration test:
[TestMethod]
public void TestMethod1()
{
using (var ctx = new MyEntitiesContext(1))
{
Assert.AreEqual(2, ctx.Roles.ToList().Count);
Assert.AreEqual(2, ctx.Users.ToList().Count);
}
}
But this requires me to leave the connection open - this is error prone, as I will always need CONTEXT_INFO, and another developer can easily do:
[TestMethod]
public void TestMethod2()
{
using (var ctx = new MyEntitiesContext(1))
{
ctx.Connection.Close();
Assert.AreEqual(2, ctx.Roles.ToList().Count);
Assert.AreEqual(2, ctx.Users.ToList().Count);
}
}
, , , - (YAY!), TestMethod2 - -, f'd up - , , :/
[EDIT2]
, , , . , NHibernate :)