Azure Functions, Entity Framework and Oracle DB - basic POC fails - visual-studio

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

Related

SSRS reports with .Net Core 3.1 MVC application

I am trying to display the SSRS report in a .Net Core 3.1 MVC application.
I tried to implement the approach mentioned in
https://alanjuden.com/2016/11/10/mvc-net-core-report-viewer/?unapproved=58532&moderation-hash=321d5350c96d2fcf83baa4c939bbdf53#comment-58532
public class ReportsController : AlanJuden.MvcReportViewer.ReportController
{
protected override ICredentials NetworkCredentials
{
get
{
//Custom Domain authentication (be sure to pull the info from a config file)
return new System.Net.NetworkCredential("username", "password");
//Default domain credentials (windows authentication)
//return System.Net.CredentialCache.DefaultNetworkCredentials;
}
}
protected override string ReportServerUrl
{
get
{
//You don't want to put the full API path here, just the path to the report server's ReportServer directory that it creates (you should be able to access this path from your browser:
return "https://YourReportServerUrl.com/ReportServer/ReportExecution2005.asmx";
}
}
public IActionResult ProcessReport()
{
var model = this.GetReportViewerModel(Request);
model.ReportPath = "reportPath";
return RedirectToAction("ReportViewer", model);
}}
but it is not working with the latest framework.
I am getting following error while running the project - Error screenshot
Any help is appreciated.
Thanks!
The same thing happened to me, in my case I needed to install the same package that tells you to install
Install-Package System.ServiceModel.Http -Version 4.1.0
or in the nuget look for the package System.ServiceModel.Http
I tried different workarounds with latest .NET Core including the one you mentioned from Alan Juden. However the easiest thing that worked for me is to create a plain .NET WebForms site using the Report Viewer control from Microsoft. It was still a lot of code but this is solid because the Report Viewer control has been around for many years.
In my case it is showing SSRS Report from Angular UI, but the same will work with MVC or any other Web UI because you will actually redirect/navigate to another url (WebForms aspx page).
More details here.

Xamarin - .NET Standard - Use Azure DevOps Services with VssAadCredential -

I want to create a Xamarin.Forms app where I have to login to a specific Azure DevOps environment/project.
To try out the Azure DevOps Service library I first created a Console App (.NET Framework 4.7.2) to login to the Azure DevOps environment/project. The following code was used for login process (+ extra code to validate the connection actualy works).
public void Login(string _userName, string _pwd)
{
ProjectHttpClient projectClient;
this.Credentials = new VssAadCredential(_userName, _pwd);
this.Connection = new VssConnection(new Uri(this.DevOpsPath), this.Credentials);
this.InitReferences(this.ProjectName);
projectClient = this.Connection.GetClient<ProjectHttpClient>();
this.ProjectReference = projectClient.GetProjects(null, top: 1).Result.Where(item => item.Name == this.ProjectName).FirstOrDefault();
}
When I use the same piece of code in the Xamarin.Forms App (.NET Standard 2.1) it no longer works and I get the following error when executing the last line:
One or more errors occurred. (Could not resolve type with token
0100008d from typeref (expected class
'Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContextIntegratedAuthExtensions'
in assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory,
Version=3.19.4.11002, Culture=neutral,
PublicKeyToken=31bf3856ad364e35'))
When using the VssBasicCredential with a personal acces token, the code runs as expected. However I would prefer using the VssAadCredential and not the VssBasicCredential.
I'm not aware that the VssAadCredential is not supported in .NET Standard and can find no documentation relating to the issue.
Has anyone had a similar experience that might solve this problem or can anyone provide me with some documentation declaring that this cannot work as of yet?

Error 0152: No Entity Framework provider found for 'Oracle.DataAccess.Client' ADO.NET provider

I have Windows 7 x64, Visual Studio 2012 with the 'ODAC 11.2 Release 5 and Oracle Developer Tools for Visual Studio (11.2.0.3.20)'.
Then I connected to Oracle db, create .edmx model. But when I'm trying to use EF to operate some data
var ent = new Entities();
var res = ent.table1.ToList();
Error happends
Schema specified is not valid. Errors:
Model.ssdl(2,2) : error 0152: No Entity Framework provider found for
'Oracle.DataAccess.Client' ADO.NET provider. Make sure the provider is registered in
the 'entityFramework' section of the application config file. See
http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
I checked my AppConfig, all sections are ok. Also I installed ODAC1120320Xcopy_x64 (and added a environment variables too). But nothing changes.
If I do it in this way, all works fine
EntityConnection econn = new EntityConnection("name=Entities");
econn.Open();
EntityCommand ecmd = econn.CreateCommand();
ecmd.CommandText = "SELECT e.NAME FROM Entities.table1 as e";
EntityDataReader ereader = ecmd.ExecuteReader(CommandBehavior.SequentialAccess);
while (ereader.Read()) { ... }
Have you any ideas? Will be very grateful for any help.

How to reference an initialized embedded RavenDB instance in a Class Library?

My scenario is this:
I have a custom RavenDB membership provider that is implemented in a class library (DLL). This provider needs to access a database to store and retrieve User and Role information. I'd like to use the same app database to store membership information to avoid having one more database.
I don't know how to get a reference to the already initialized database (app database) inside the class library code. I think I'm going the wrong way here... :)
Some code:
bool embeddedStore = Convert.ToBoolean(config["enableEmbeddableDocumentStore"]);
if (embeddedStore)
{
_documentStore = new EmbeddableDocumentStore()
{
// Here I'm using the same connection string used by the app.
// This gives me an error when I try to open a session in the DocumentStore.
ConnectionStringName =
config["connectionStringName"]
};
}
else
{
_documentStore = new DocumentStore()
{
ConnectionStringName =
config["connectionStringName"]
};
}
This is the connection string present in Web.config:
<add name="RavenDB" connectionString="DataDir = ~\App_Data\Database" />
How can I reuse the same database within the custom membership provider? Any ideas?
I thought about moving the class library code files to the Web project. This way I could get a reference to the DocumentStore easily, but the code wouldn't be as organized as I'd like.
I also tried to use 2 RavenDB databases: 1 for the app and 1 for the membership provider, but as I'm running RavenDB in its embeddable fashion I couldn't get it working.
These are the errors I got during my attempts so far:
RavenDB Could not open transactional storage.
Temp path already used by another database instance.
You need to pass the instance of the opened document store to your dll.
You can do that using a container or by providing an API call to do that.
You can't have two instance using the same db.

How to use new database instance of each MSTest unit test run

I'm using MSTest under Visual Studio 2010 to test an ASP.NET MVC 3 project. I have a SQL Express 2005 DB that I'd like it to use and I want a fresh instance of the DB every time, copied from a template I've included in the project. Pretty standard requirements, I would have though, but I can't get this to work.
I've created a .testsettings file which enables deployment and my connection string looks like this:
<add name="MyDb" connectionString="Data Source=.\SQLEXPRESS2005;Database=MyDbTest;AttachDBFilename=|DataDirectory|MyDbTest.mdf;User Instance=true;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
The test runs OK the first time, but after that it fails with errors like this:
Test method ... threw exception: System.Data.DataException: An exception occurred while initializing the database. See the InnerException for details. ---> System.Data.EntityException: The underlying provider failed on Open.
---> System.Data.SqlClient.SqlException: Database '...\bin\Debug\MyDbTest.mdf' already exists. Choose a different database name. Cannot attach the file '...\Out\MyDbTest.mdf' as database 'MyDbTest'.
The accepted answer in this MSDN thead says to remove the "Database=" connection string parameter. However, if I do that it fails with this error:
Test method ... threw exception:
System.InvalidOperationException: Unable to complete operation. The supplied SqlConnection does not specify an initial catalog.
How do I get this to work?
So far I've come up with a hack to change the DB name at runtime, at test assembly initialization time. This requires a further hack - using Reflection to enable modifying the configuration at runtime (thanks to David Gardiner).
[TestClass]
public static class TestHelper
{
[AssemblyInitialize]
public static void AssemblyInit(TestContext context)
{
RandomizeDbName();
}
private static void RandomizeDbName()
{
// Get the DB connection string setting
var connStringSetting = ConfigurationManager.ConnectionStrings["TheDbSetting"];
// Hack it using Reflection to make it writeable
var readOnlyField = typeof(ConfigurationElement).GetField("_bReadOnly",
BindingFlags.Instance | BindingFlags.NonPublic);
readOnlyField.SetValue(connStringSetting, false);
// Randomize the DB name, so that SQL Express doesn't complain that it's already in use
connStringSetting.ConnectionString = connStringSetting.ConnectionString.Replace(
"Database=MyTestDb", "Database=MyTestDb_" + new Random().Next());
}
}
Edit: It's actually even a bit worse than this: I'm having to call TestHelper.RandomizeDbName() at the start of every test that requires a fresh DB, otherwise it gets data left over from previous tests.

Resources