System.InvalidOperationException: No connection string named 'xxx' could be found in the application config file - entity-framework

I am using EF 6 db first and asp,.net core web api. I found too stackoverflow post but no use.
The flow is WebAPI(Asp.net core)->BL->DAL(EDMX)

Related

ASP.NET Core Rest API Saving Failed saying no access to another database

My ASP.NET Core Rest API is returning a 500 Internal Server Error on post method while adding a new customer. I am able to get data from my development database, but myDbContext.SaveChanges(); failed saying my development user doesn't have access to the main database, but my connection string refers to my development database only.
My database name : MyappDbDev
My database user : AppUserDev
Error:
The server principal "AppUserDev" is not able to access the database "MyappDbDev" under the current security context
I have another database in production with following credentials:
Database name : MyappDb
Database user : AppUser
I didn't find any useful suggestions after researching for days, at last I found that I had a trigger for the table that I am adding new record.
which the trigger had insert command to old database.
It was a silly mistake , but I hope this may help someone someday.

Passing appsettings.json connection string from .Net Core Web API to class library

I am working on a requirement where I have a .Net Core Web API and a class library project as DAL layer which connects to SQL DB. I have defined SQL connection string in appsettings.json and I want to use it in class library when I have to make a connection with SQL DB. I am using ADO.Net for establishing SQL connection here.
Can anyone please help me on how to pass on connection string value from Web API project to DAL layer?

Azure SQL Database connection using Active Directory Integrated authentication fails to open

I'm attempting to connect to Azure SQL Database via Entity Framework with a connection string similar to this:
Data Source=<server>.database.windows.net;Authentication=Active Directory Integrated;Initial Catalog=<database>
The connection attempt is made within the context of a hosted WPF form running inside AutoCAD 2018. The project is built using .NET Framework 4.6 and EF 6.1.3.
I'm encountering the following error:
Unable to load adalsql.dll (Authentication=ActiveDirectoryIntegrated). Error code: 0x2. For more information, see http://go.microsoft.com/fwlink/?LinkID=513072
Unfortunately the help link doesn't lead to page that provides technical details for this issue. I haven't so far discovered anything on the web elucidating the root cause.
I've also tried this connection string:
Server=tcp:<server>.database.windows.net,1433;Initial Catalog=<database>;Persist Security Info=False;User ID=<username>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Authentication="Active Directory Integrated"
It was copied from the Azure portal's ADO.NET (Active Directory integrated authentication) section. However, with this connection string I get the following error Cannot use 'Authentication=Active Directory Integrated' with 'User ID', 'UID', 'Password' or 'PWD' connection string keywords
But it doesn't contain a password segment. And after removing the User ID segment, I still get the Unable to load adalsql.dll ... error.
The connection string
Server=<server>.database.windows.net,1433; Authentication=Active Directory Integrated; Initial Catalog=<database>;
works if you follow this hack:
/// <summary>
/// HACK: Refer to https://stackoverflow.com/a/19130718/852956
/// </summary>
public void FixEfProviderServicesProblem() {
//The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer'
//for the 'System.Data.SqlClient' ADO.NET provider could not be loaded.
//Make sure the provider assembly is available to the running application.
//See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
SqlProviderServices instance = SqlProviderServices.Instance;
}

Web app with EF + database first not working on Azure

I have a ASP.NET MVC application which uses EF (v6) as data access layer. My application works fine on IIS Express and also when deployed to the server running IIS 7.5.
The problem is that I'm getting the following exception when I deploy it to Azure (Web Sites).
Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.
I've searched the web and I made sure my connection string starts with "metadata=" + checked my db context class' constructor to be sure it contains the correct name (in my case it's "name=PsDataEntities"))
My connection string looks like this: <add name="PsDataEntities" connectionString="metadata=res://*/PsDataModel.csdl|res://*/PsDataModel.ssdl|res://*/PsDataModel.msl;
provider=System.Data.SqlClient;
provider connection string="data source=SERVER_NAME;initial catalog=DB_NAME;user id=UID;password=PWD;
MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Anybody had the same problems?
Any help is appreciated
I did a few more tests (like changing metadata in the connection string to use fully qualified names instead of '*') without success, then I decided to set my metadata (regarding .csdl/.ssdl/.msl) programatically with the help from this post. Application now works correctly when deployed to Azure.

Using SQL Server CE 4.0 with Entity Framework on Windows Azure

I am using SQL Server CE 4.0 with WebApi on Windows Azure Websites. I have been successfully able to deploy SQL Server CE. The weird problem I am facing is that my site is able to log me in using the same DB but I am not able to use any of the controllers to fetch the data.
I am using same connection string for both. The only difference is that for logging in I am using WebSecurity as I have enabled OAuth on the site.
Can someone throw some light on how to debug and fix this issue? The error I am getting for the calls is
Format of the initialization string does not conform to specification
starting at index 0.
However the same string works for authentication, change password, adding OAuth connections etc.
Thanks in advance
I connected to the site using FTP. I was not giving the site name as domain name and it was denying me access earlier. On connecting, I got hold of the Web.config file and I found something interesting. While publishing the site, the web.config was modified to add another connectionstring with the name of context_DatabasePublish.
This string had following details connectionString="ContextName_DatabasePublish.ConnetionString" providerName="System.Data.SqlClient"
Also there was a new section called context added to the entityframework section of the config file with all the details for the context to use but again pointing to same connection string. The provider it is using is sql and not sqlce. I believe that is the reason it was failing.
I uploaded my normal config file and the site started working. I need to explore more on to why and how the new connection string got added. I will post the details in comments.