I've created an autohosted MVC 4 app to embed into a Sharepoint 365 account.
I have followed the tutorial here: http://msdn.microsoft.com/en-us/library/fp179902.aspx to the letter AFAIK.
The application is standalone, I just need to embed it into Sharepoint for the correct people to have access.
Here is my Home Controller:
namespace ResourceTrackerWeb.Controllers
{
public class HomeController : Controller
{
static SqlConnection GetActiveSqlConnection()
{
return new SqlConnection(GetCurrentConnectionString());
}
static string GetCurrentConnectionString()
{
return WebConfigurationManager.AppSettings["SqlAzureConnectionString"];
}
private ResourceTrackerWebContext db = new ResourceTrackerWebContext(GetActiveSqlConnection(), true);
[SharePointContextFilter]
public ActionResult Index()
{
User spUser = null;
var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
using (var clientContext = spContext.CreateUserClientContextForSPHost())
{
if (clientContext != null)
{
spUser = clientContext.Web.CurrentUser;
clientContext.Load(spUser, user => user.Title);
clientContext.ExecuteQuery();
ViewBag.UserName = spUser.Title;
}
}
var projects = db.Projects.Include(p => p.Customer).Include(p => p.JobType).Include(p => p.Location).Include(p => p.ProjectStatus);
return View(projects.ToList());
//return View();
}
}
}
If I uncomment the last return View() and comment out the projects variable load, I work fine. Once I use the DbContext, I get an "Unknown User" error. This seems to be something with using the SharepointContext and DbContext at the same time.
Also, the code works fine in debug mode. When I deploy it to Sharepoint 365, this happens.
I also altered the init function for my ResourceTrackerWebContext as such:
public ResourceTrackerWebContext(SqlConnection conn, bool contextOwnsConnection) : base("name=ResourceTrackerWebContext")
{
}
So my question is. How do I configure my DbContext to work with my Azure database in this case? The only answer I've found is to manually rewrite all of my Controllers to access the SqlConnection and query the db from there. Is there a way to do this without rewriting the functionality of the Controllers?