entity connection error with EF6 and MySQL connector - c#

I'm getting the following error over and over, despite all attempts to resolve the issue.
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid
The main issue seems to connect to the EF over several projects in 1 solution.
Here's what I've already tried:
copied the connection string to the executing project in de web.config file
added references to the mysql providers
changed connection string names to the container name
Still get stuck at the construction of my ObjectContext where I get the error above.
public EcomContainer() :
base(#"name=ConString", "EcomContainer")
{
Configure();
}
I'm using the Devart mysql dotnet connector, EF6
My testunit is a regular C# console app which works fine with EF
but a new project with a web app such as a wep api or mvc application not.
If anyone would have some experience with this, I welcome any tips.
Thanks

Related

Entity Developer/EntityFramework - The specified named connection is either not found in the configuration

Using Devart Entity Developer with Entity Framework 6. I have my main class library project. I had a different Winform project altogether that uses Entity Developer/Entity Framework that I developed to do some database work with Sql. I decided to add this secondary Winform project to the main class library.
First I tried adding the Winform project to the solution. That did not work well. Even though I added a reference to the exe of the Winform project it seems not to find it ok. I removed that project from the solution.
As a second attempt. I copied the classes from the Winform project (two of them) to the main class library project and recreated the Entity Developer/Entity Framework stuff. Now when I run the project everything runs (including the new form) but I cannot connect to the database. I get the error:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
The line that has the error is the line calling base:
public SWPDMEngVaultEntities() :
base(#"name=SWPDMEngVaultEntitiesConnectionString", "SWPDMEngVaultEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
My connection string is in the app.config file and looks like this:
<connectionStrings>
<add name="SWPDMEngVaultEntitiesConnectionString" connectionString="metadata=res://*/DataModel1.csdl|res://*/DataModel1.ssdl|res://*/DataModel1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=XXXXXX\SQLEXPRESS;Initial Catalog=XXXXXX;Integrated Security=False;Persist Security Info=True;User ID=XXXXXX;Password=XXXXXX""
providerName="System.Data.EntityClient" />
</connectionStrings>
All of the XXXXXX's in the above string have been confirmed as correct. Also, Entity Developer has a "Test Connection" and the test connection connects just fine. Entity Developer also gets the database information OK so I know it is able to connect without issue. The problem is at runtime on the solution.
I have looked at other posts about this error and they all seem to be saying make sure your connection string is good. What else should I check?
The answer to this issue came from Devart. Class libraries do not use app.config files for settings. By default the connection string in the Devary generated templates are stored in the app.config file. There is a place to turn this off when the model is generated.

Snowflake Net Connector Connection string is invalid

I'm trying to connect to our Snowflake DB using SSO with the ff connection string but keeps on getting an error. I'm using https://github.com/snowflakedb/snowflake-connector-net for my .NET 4.6.1 console application just to test things out. I have confirmed that my SSO is able to login to the Snowflake Web UI. Found related docs: https://docs.snowflake.com/en/user-guide/admin-security-fed-auth-use.html#native-sso-okta-only
connection string:
AUTHENTICATOR=https://{okta_account_name}.okta.com;ACCOUNT={org}.{region};HOST={org}.{region}.snowflakecomputing.com;PORT=443;ROLE={role};WAREHOUSE={warehouse};USER={sso email};password={sso password};DB={db};SCHEMA={schema}
error:
Exception: Error: Connection string is invalid: Unable to connect SqlState: 08006, VendorCode: 270008, QueryId:
Maybe I posted a question too soon. Following the Readme.md for Snowflake Dotnet Connector, I was able to generate the logs thru log4net. The underlying exception was
[2021-08-11 23:09:04,732] [1] [ERROR] [Snowflake.Data.Core.SFSession] Unable to connect
System.PlatformNotSupportedException: Operation is not supported on this platform.
at Snowflake.Data.Core.HttpUtil.setupCustomHttpHandler(HttpClientConfig config)
at Snowflake.Data.Core.HttpUtil.<>c__DisplayClass10_0.<RegisterNewHttpClientIfNecessary>b__1()
at Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.<>c__DisplayClass5_0.<ConfigurePrimaryHttpMessageHandler>b__1(HttpMessageHandlerBuilder b)
at Microsoft.Extensions.Http.DefaultHttpClientFactory.<>c__DisplayClass17_0.<CreateHandlerEntry>g__Configure|0(HttpMessageHandlerBuilder b)
at Microsoft.Extensions.Http.LoggingHttpMessageHandlerBuilderFilter.<>c__DisplayClass3_0.<Configure>b__0(HttpMessageHandlerBuilder builder)
at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateHandlerEntry(String name)
at Microsoft.Extensions.Http.DefaultHttpClientFactory.<>c__DisplayClass14_0.<.ctor>b__1()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Lazy`1.get_Value()
at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateHandler(String name)
at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateClient(String name)
at Snowflake.Data.Core.HttpUtil.GetHttpClient(HttpClientConfig config)
at Snowflake.Data.Core.SFSession..ctor(String connectionString, SecureString password)
After browsing the web with similar exception it was mostly about the target .NET framework not satisfying the requirement. I was initially targeting .NET Framework 4.6.1 but after changing it to 4.7.2 everything worked fine as expected. It was indeed mentioned on the repo that the driver is now targeting 4.7.2 but I don't recall seeing it being unable to support lower framework versions. I tried downgrading the driver to the earliest version and upgrading it to the latest one by one with no avail. Hope this help others facing the same issue.
Based on my experience with Snowflake.Data, it supports .Net framework starting from version 4.6.1.
The connection string is like:
connStr="scheme=https;account=youraccount;host=xxx.snowflakecomputing.com;port=443;proxyhost=xx.myproxy.com;proxyport=80;role=MY_ROLE;warehouse=WAREHOUSE_NAME;user=userid;password=xxx;authenticator=xxx.okta.com"
You may also try to set authenticator=externalbrowser.
I personally prefer to connect to Snowflake using ODBC Driver. Please refer to https://docs.snowflake.com/en/user-guide/odbc.html on how to install ODBC driver in your local machine. Once you install and configure Snowflake in your machine, you may connect to Snowflake using System.Data.Odbclibrary.
The connection string might looks like:
connStr="Dsn=DataSourceName;uid=userID;pwd=xxx"
You may also try browser-based SSO:
In the connection string, set AUTHENTICATOR=externalbrowser, and set USER to the login name for your IdP.
using (IDbConnection conn = new SnowflakeDbConnection())
{
conn.ConnectionString = "account=testaccount;authenticator=externalbrowser;user={login_name_for_IdP};db=testdb;schema=testschema";
conn.Open();
conn.Close();
}
where:
{login_name_for_IdP} is your login name for your IdP.

MongoDB :Auth mechanism not specified Error for specific Project

We running 2 web .net project on same Pc and connecting to same Mongod server .
first and second project connect to same database.
First project with authorization connection string working fine without connection issue
but Second project with same authorization string giving Auth mechanism not specified error
if I specified any auth mechanism then it giving
Unsupported MongoAuthenticationMechanism SCRAM-SHA-1 error
This is happen only for second project , not issue with first project
I am sure that
both mongod server and shell version match
same connection string for both
but why it giving issue for second project only. what would be possible project level issue? any idea.
Please help for this
Resolved this issue, MongoDb DLL issue which is different for both projected. Used one which is working for both.

ASP.NET Core 2 Migrations don't work on new machine with empty database

I created a ASP.NET Core 2 project, created some migrations along the way, and pushed the code up to a repository.
Today, I pulled the code to another computer I have for development, the database was obviously not created on the new machine so I created a empty database locally to match the connection string.
Now, when trying to run the code "dotnet ef database update" I get this error:
An error occurred while calling method 'BuildWebHost' on class
'Program'. Continuing without the application service provider. Error:
One or more errors occurred. (Invalid object name 'AspNetUsers'.)
Unable to create an object of type 'AppDbContext'. Add an
implementation of 'IDesignTimeDbContextFactory' to the
project, or see https://go.microsoft.com/fwlink/?linkid=851728 for
additional patterns supported at design time.
Please keep in mind that any "dotnet ef" command I run I have the same error, I tried deleting all the migrations and creating a new one using the "dotnet ef migrations add InitialDb" command, but I still get this error.
I am using Identity in my project and I believe it has to do with this error: (Invalid object name 'AspNetUsers'.)
The commands run without any problem on the machine I initially started to develop the application.
I tried Update-Database from PM with the same result.
Here is an image illustrating the problem:
Thanks in advance.
I found the problem, I had a seeder in the Startup class that was seeding an admin user in case it did not exist.
Because the table did not exist the seeder was failing on each command because it was in the Startup class and was executing all the time.
This issue can be closed.

Entity: automatic migration on Production not working

I have looked at other questions on SO and on blogs and official MS docs but I can't find the proper way.
What I am trying to achieve:
Deploy version 1 of an MVC website with SQL DB (code is based on Entity Code First v. 6.1), without any concept of migration because it's DB SCHEMA v.1
Deploy further versions (2, 3, ..) with automatic migration to SCHEMA v2 (and then v3 etc.) and without running Update-Database in Package Manager console
So, following MS tutorial (i.e. https://msdn.microsoft.com/en-gb/data/jj591621.aspx) version 1 creates tables for Schema v1:
Blogs table with two columns (BlogId and Name)
__MigrationHistory with one row (MigrationId contains InitialCreate). Not sure what this is about, but I guess it's the initial schema. Am I right?
Then I open Package Manager Console and I type: Enable-Migrations which creates classes for migration (201610091823367_InitialCreate.cs and Configuration.cs). I then modify the Blog class to have a new property Url as suggested in the tutorial and run Add-Migration AddBlogUrl, which creates 201610091830008_AddBlogUrl.cs
In addition, I added
Database.SetInitializer(new MigrateDatabaseToLatestVersion<BlogContext, Configuration>());
to the main, so when the application starts it should migrate automatically.
I launch the app v2, and everything is working perfectly. The Table has now the new column.
When I launch the app v2 again, it crashes with exception:
Unhandled exception: System.Data.SqlClient.SqlException: There is already an object named 'Blogs' in the database. and all the related stack trace.
So it looks like it's not recognizing that it's in the DB there's the most recent schema, so it tries to create the table again. I have added a breakpoint to InitialCreate class, method Up and it is called. I am surprised!
I've tried also a different approach:
ran app v1
ran app v2b with AutomaticMigrationsEnabled = true; in the Configuration class. The outcome is the same.
Can anyone help me to understand what's wrong and what's the right approach?
Can you confirm that in the meantime I am OK to deploy SCHEMA v1 without having enabled migrations at all?
I was hoping that this kind of scenarios were covered by Entity.

Categories