I have an MVC5 application with EF 6.1 and the basic forms work great. Need to develop some complex forms though so webforms seemed the better option. Followed all the advice on converting DbContext to ObjectContext in order to use EntityDataSource for database communication but just get the 'Cannot implicitly convert type System.Data.Entity.Core.Objects.ObjectContext to System.Data.Objects.ObjectContext' error despite updating to the latest version which is supposed to resolve this issue.
protected void EntityDataSource_ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e)
{
var db = new myDbContext();
e.Context = (db as IObjectContextAdapter).ObjectContext;
//also tried e.Context = ((IObjectContextAdapter)db).ObjectContext;
}
Would appreciate any advice on getting webforms running alongside MVC5, especially on getting the connection set up correctly for insert.
Related
I'm trying to scaffold a new razor view using Visual Studio. I select a template, my model and my DbContext, then I get the error message shown below.
Things to note. My models, my DbContext and my website are all in different projects. From the message below I am using AddDbContext and I have a constructor that accepts a DbContextOptions<TContext> parameter.
I read a comment on a blog post that the issue is because my context is in another project. The comment referenced something about the need to inject the Configuration into the DbContext to get the connection string and manually add it in the OnConfiguring override.
I can't find any examples if this is correct or how to set it up. Any help would be appreciated.
EDIT:
Testing out the theory from the blog comment I mentioned above, I added this section into my DbContext. ConnectionString is a hardcoded string constant with my connection information. This does work and allow me to scaffold, so the question still remains. How can I inject this connection string into my DbContext to allow the scaffolding to work?
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(ConnectionString);
}
else
{
base.OnConfiguring(optionsBuilder);
}
}
EDIT: So after making this change, I checked in the code and had another developer pick it up. It appears this section above just needs to be there to allow scaffolding to work. He never changed the connection string to point to his environment. He no longer got the error above it just worked.
I am not sure about what is the actual problem but it seems like we were having problems creating DbContext at design time. I manually added the code below and it's working now. It's just a temporary solution tho.
public AppDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=JwtTemplate;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
return new AppDbContext(optionsBuilder.Options);
}
Reference: https://stackoverflow.com/a/70559350
I'm having a lot of trouble getting a basic proof-of-concept working, in which I am accessing an Oracle DB (11g) through Azure Functions via Entity Framework (6.2).
Prerequisites:
ODT For Visual Studio 2017 is installed, as well as Azure Functions CLI/Core Tools. Everything mentioned below is done entirely via Visual Studio 2017, not through Azure portal.
Take 1:
Created a new project with the Azure Functions template.
Installed NuGet packages EntityFramework (6.2.0), Oracle.ManagedDataAccess (12.2.1100) and Oracle.ManagedDataAccess.EntityFramework (12.2.1100). Note: When installing NuGet packages in projects using the Azure Functions template, the packages are added under Dependencies -> NuGet, rather than under References.
Added ADO.NET Entity Data Model to project.
Problem: After setting my connection string, choosing Entity Framework 6.x is unavailable, with the following error message:
An Entity Framework database provider compatible with the latest
version of Entity Framework could not be found for your data
connection. If you have already installed a compatible provider,
ensure you have rebuilt your project before performing this action.
Otherwise, exit this wizard, install a comaptible provider, and
rebuild your project befre performing this action.
As the simplest of workarounds, I have tried to just go ahead with EF5, but it throws an exception while creating the DB model (after selecting the objects to include in model, including some stored procedures).
Take 2:
Created project and installed NuGet packages as above.
Created class library project to facilitate the Oracle interactions.
Installed the same NuGet packages as above in the class library project.
Added ADO.NET Entity Data Model to class library project and added some database objects to the database model. Also added custom constructor to the model for specific connection string, because managing connection strings in Azure Functions was a seperate set of headaches that I'll deal with later.
Added a simple wrapper method to the class library project that calls a stored procedure from the database model:
public static string NameByEmpNo(int empNo)
{
string result;
MyEntities entities = new MyEntities("metadata=res://*/MyEntities.csdl|res://*/MyEntities.ssdl|res://*/MyEntities.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string='DATA SOURCE=127.0.0.1:1521/ORCL;PASSWORD=tiger;USER ID=SCOTT'");
ObjectParameter name = new ObjectParameter("o_empname", typeof(string));
entities.GET_EMP_NAME_PROC(empNo, name);
result = (string)name.Value;
return result;
}
Added reference to the class library in the Azure Functions project.
Added function that calls NameByEmpNo:
[FunctionName("GetNameByEmpNo")]
public static async Task<HttpResponseMessage> GetNameByEmpNo([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequestMessage req, TraceWriter log)
{
int empNo = Int32.Parse(req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "empno", true) == 0)
.Value);
string empName = ScottAccess.NameByEmpNo(empNo);
return req.CreateResponse(HttpStatusCode.OK, "Employee name: " + empName);
}
Problem: At runtime, calling the function fails with this error
message:
Exception while executing function: GetNameByEmpNo -> The ADO.NET
provider with invariant name 'Oracle.ManagedDataAccess.Client' is
either not registered in the machine or application config file, or
could not be loaded. See the inner exception for details. -> Unable to
find the requested .Net Framework Data Provider. It may not be
installed.
Bonus info: My class library works perfectly when called through a console application. Also, my Azure Functions app works perfectly when calling functions that do not use my class library...
I am stumped. Has anyone got experience with getting this combination of techs working together and can offer some insight into where I'm going wrong / provide steps to get a basic connection working?
Entity Framework within Azure Functions defaults the providers to System.Data.SqlClient so SQL connections will work without any configuration changes, but that means you have to do something special for Oracle connections. The problem seems to come from the config values that the Oracle.ManagedDataAccess.Client library assumes are available within the App.Config or Web.Config file in the project, which are inserted whenever you install the Oracle.ManagedDataAcess.EntityFramework Nuget package. Azure Functions don't have config files, and I wasn't able to find any way to specify the Oracle provider in the settings json files.
I found a solution in this post
It suggests bypassing this mechanism and creating a DbConfiguration for Oracle, then using DbConfigurationType to tell the DbContext which configuration you're using.
public class OracleDbConfiguration : DbConfiguration
{
public OracleDbConfiguration()
{
SetDefaultConnectionFactory(new OracleConnectionFactory());
SetProviderServices("Oracle.ManagedDataAccess.Client", EFOracleProviderServices.Instance);
SetProviderFactory("Oracle.ManagedDataAccess.Client", new OracleClientFactory());
}
}
[DbConfigurationType(typeof(OracleDbConfiguration))]
public partial class MyEntities : IGISContext
{
//Expose Connection String Constructor
public MyEntities(string connectionString, int commandTimeoutInSeconds = 30) : base(connectionString)
{
this.Database.CommandTimeout = commandTimeoutInSeconds;
}
}
Note: I used EF 6 Database First to generate my EDMX; MyEntities here is a partial class for providing a constructor that takes in a connection string.
The oracle connection wil use the specified DbConfiguration class, and any SQL database connections will continue to work using the defaults.
My solution is using the Nuget Packages:
EntityFramework 6.2.0
Oracle.ManagedDataAccess 12.2.1100
Oracle.ManagedDataAccess.EntityFramework 12.2.1100
I've been working on my first localization project for mvc and have been using the excellent Griffin.MvcContrib to get me started.
I've using the Griffin to handle the language change and the page views, and have a custom provider setup to handle the models and validation through database resources.
The validation is done with FluentValidation like the following:
RuleFor(x => x.Bin)
.Length(0, 50)
.WithMessage(localizationService.GetResource("Inspection.Bin.Length"));
Everything seemed to work great until I realized that server side errors were not being translated, only the client side ones. After a lengthy investigation it seems to me the issue is that the thread culture is being set after the server side validation is done - I believe the same thing that was happening in this other post:
MVC3 globalization not set for ModelState.Value.Culture
What I did to get it working so far is just to place the following in the Global.asax file.
protected void Application_BeginRequest(object sender, EventArgs e)
{
string CookieName = "GriffinLanguageSwitcher";
HttpContextBase currentContext = new HttpContextWrapper(HttpContext.Current);
if (currentContext.Request.Cookies[CookieName] != null)
{
Thread.CurrentThread.CurrentCulture =
Thread.CurrentThread.CurrentUICulture =
new CultureInfo(currentContext.Request.Cookies[CookieName].Value);
}
}
It seems to work. I'm not worried about validation when a user switches their language, and it now seems to pickup the current language and translate the server side errors.
But is this an acceptable way of fixing this issue, or should I be doing something else. Thanks.
I'm running a mixed MVC Application inside a sub folder of a web forms application.
Everything worked great in VS 2010 debug (Cassini) but when I deployed to IIS7.5
I got the following error:
'HttpContext.SetSessionStateBehavior' can only be invoked before
'HttpApplication.AcquireRequestState' event is raised.
It errors on the last line (httpHandler.ProcessRequest(HttpContext.Current);) in the default.aspx file of the MVC application sub folder.
public void Page_Load(object sender, System.EventArgs e)
{
string pathToRewriteTo = Request.Path.ToLowerInvariant().Replace("default.aspx", "Home/Index");
HttpContext.Current.RewritePath(pathToRewriteTo, false);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
}
However if I manually navigate to Home/Index from the MVC root folder I can see my application fine from there.
I've looked up the error being thrown and I only find answers dealing with server transfers and not MVC routes.
I have also already checked my IIS7.5 configuration for the route handling module, Application pool running in integrated mode, etc.
Any help would be appreciated.
We faced a similar issue. There are changes to MVCHttpHandler in MVC2 and above.
You need to change it to use httpContext.Server.TransferRequest.
Try the below snippet:
var httpContext = HttpContext.Current;
httpContext.Server.TransferRequest(Url, true); // change to false to pass query string parameters if you have already processed them
I am facing a problem that drives me crazy for couple of days now, hoping someone can help me.
Here it is ;
I'm using EF4 with oracle database, using dotConnect for oracle from devart as provider.
I have wcf service method which calls DeleteCabinet method below;
public void DeleteCabinet(string pRID)
{
using(TransactionScope tranScope = new TransactionScope())
{
DBUtils.DeleteCabinetAndShelves(pRecordId);
//throw exception to test record not deleted
throw new Exception("xxx something has happened test xxx");
tranScope.Complete();
}
}
DBUtils.DeleteCabinetAndShelves looks like below;
public void DeleteCabinetAndShelves(string pRecordId)
{
using(var context = new EdrmEntities())
{
var cabinet = context.Cabinets.Include("Shelves").Single(p => p.RID == pCabinetRID);
//mark all cabinet shelves for deletion
if (cabinet.Shelves != null)
{
foreach (var tempShelf in cabinet.Shelves.ToList())
{
context.DeleteObject(tempShelf);
}
}
//mark cabinet for deletion
context.DeleteObject(cabinet);
//save
context.SaveChanges();
}
}
when I call DeleteCabinet from within my test project, not a wcf call but direct method call, it works OK. It throws exception ,and transaction is rolled back. Thus no record is deleted from DB as expected
The problem is that when I call service method (which calls DeleteCabinet) from a client, the exception is thrown , but the record IS deleted from db. Transaction does not roll back !
seems like calling wcf method does not roll back transaction, but it seems crazy ( at least to me), does anybody know the reason why this might be happening ?
Thanks in advance
You tagged your post with the DevArt and DotConnect tags... I wonder if this is a bug in the DevArt providers rather than something inherent to WCF / Entity Framework / System.Transactions. You could test the theory by seeing if it happens with a ObjectContext that is using the built-in SQL Server Provider (or even Oracle's own EF provider that was recently released) and see if the issue still occurs. That is the only thing I can think of since the code seems 100% correct.
Thanks to #Rabid and #luksans constructive comments problem is solved, and it turned out it has nothing to do with wcf or devart's provider being buggy
Here's the thing ; wcf service (which did not rollback), and integration test (which did) are inside different projects, thus config files are different. They got out of sync sometime in past, and the difference is in Enlist=false part. So wcf project's connection string has Enlist=false while test project does not. That's how the illusion of WCF failing transaction was born.
Removing Enlist=false from wcf project's connection string fixed the issue.