I want to have a global method that all of my controllers can access to it. in this method I want to connect to Database as I connect in my controllers. following code works fine in my controllers but when i move this code to App_Code it has some Error ! I need this method in all of my controllers so what do I do ? instead of .FirstOrDefault() I have .FirstOrDefaultAsync() ! why ?
using Sitaad.Models;
using System.Web.Mvc;
namespace Sitaad.App_Code
{
public class Reg
{
private string _lang = "Fa";
private ApplicationDbContext db = new ApplicationDbContext();
public string T(string txt)
{
string query = "select Text from Translations where Code = '@p1' and TranslateId = (select TranslateId from Translations where Text = '@p2')";
Translations t = db.Translation.SqlQuery(query,
TurboSofts.Lang.T.GetLang(),
txt).FirstOrDefault(); /* this line can't run */
return t.Text;
}
}
}