question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EF 6.3 with EDMX on .NET Core 3 in class library fails.

See original GitHub issue

I have 2 class libraries, netstandard 2.1. One contains the entity classes, the other contains the datacontext and the EDMX file (through EntityDeploy).

I use these in a class library targeting .net core 3.0 GA, which contains my unit tests. But I don’t know how to get it working, as it doesn’t accept my connection string. The example in the ‘docs’ (well, there are almost no docs on using EF 6.3 on .net core… it’s not as if the old .net framework docs work for this… ) points to https://github.com/efcore/EdmxDotNetCoreSample/ which is charming but which is an exe and which uses an app.config file which is ignored in class libraries.

My test code:

using System.Linq;
using NUnit.Framework;
using NW26NS21.EDMX;

namespace EFNS21Tester
{
    [TestFixture]
    public class EDMXFetchTests
    {
        private static string ConnectionString = "metadata=res://*/Northwind26.csdl|res://*/Northwind26.ssdl|res://*/Northwind26.msl;provider=System.Data.SqlClient;provider connection string="data source=nerd.sd.local;initial catalog=Northwind;integrated security=SSPI;persist security info=False"";

        [Test]
        public void FetchAllCustomersFromUSA()
        {
            using(var ctx = new Northwind26DataContext(ConnectionString))
            {
                var customers = ctx.Customers.Where(c => c.VisitingAddress.Country == "USA").ToList();
                Assert.AreEqual(13, customers.Count);
            }
        }
    }
}

This gives me:

System.ArgumentException : Keyword not supported: 'data source'.
   at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions.ParseInternal(IDictionary`2 parsetable, String connectionString, IList`1 validKeywords)
   at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions..ctor(String connectionString, IList`1 validKeywords)
   at System.Data.Entity.Core.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
   at System.Data.Entity.Core.EntityClient.EntityConnection..ctor(String connectionString)
   at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
   at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
   at EFNS21Tester.EDMXFetchTests.FetchAllCustomersFromUSA() in C:\MyProjects\VS.NET Projects\LLBLGen Pro v5.6\UnitTests\EntityFramework\EFNS21Tester\EDMXFetchTests.cs:line 22

I’ve also tried (of course) the setup of the example, with a config file. I then do:

[Test]
public void FetchAllCustomersFromUSA()
{
    using(var ctx = new Northwind26DataContext())
    {
        var customers = ctx.Customers.Where(c => c.VisitingAddress.Country == "USA").ToList();
        Assert.AreEqual(13, customers.Count);
    }
}

Which defaults to:

	public partial class Northwind26DataContext : DbContext 
	{
		partial void OnContextCreated();
		
		
		/// <summary>Initializes a new instance of the <see cref="Northwind26DataContext"/> class using the connection string found in the 'Northwind26' section of the application configuration file.</summary>
		public Northwind26DataContext() : base("name=ConnectionString.SQL Server (SqlClient)")
		{
			Initialize();
		}
		
		/// <summary>Initializes a new instance of the <see cref="Northwind26DataContext"/> class</summary>
		public Northwind26DataContext(string connectionString) : base(connectionString)
		{
			Initialize();
		}

and that results in:

System.InvalidOperationException : No connection string named 'ConnectionString.SQL Server (SqlClient)' could be found in the application config file.
   at System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel()
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
   at EFNS21Tester.EDMXFetchTests.FetchAllCustomersFromUSA() in C:\MyProjects\VS.NET Projects\LLBLGen Pro v5.6\UnitTests\EntityFramework\EFNS21Tester\EDMXFetchTests.cs:line 18

however the project does have an app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<connectionStrings>
		<!-- please adjust the connection string embedded in the element below to target the proper catalog / server using the proper user / password combination -->
		<add name="ConnectionString.SQL Server (SqlClient)" connectionString="metadata=res://*/Northwind26.csdl|res://*/Northwind26.ssdl|res://*/Northwind26.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=nerd.sd.local;initial catalog=Northwind;integrated security=SSPI;persist security info=False&quot;" providerName="System.Data.EntityClient" />
	</connectionStrings>
</configuration> 

But as this is a class lib with tests, it likely is ignored (as .net core doesn’t really use app.config files to my knowledge)

If I pass a normal connection string like in Code First, I get a nice message telling me the context is in Code First mode and I should go to an external URL, which brings me to the universal page to ‘get started with EF 6’ 😄

System.Data.Entity.Infrastructure.UnintentionalCodeFirstException : The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development.  This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715
   at NW26NS21.EDMX.Northwind26DataContext.OnModelCreating(DbModelBuilder modelBuilder) in C:\MyProjects\VS.NET Projects\LLBLGen Pro v5.6\UnitTests\EntityFramework\NWDbContextNS21EDMX\DAL\Persistence\Northwind26DataContext.cs:line 47
   at System.Data.Entity.DbContext.CallOnModelCreating(DbModelBuilder modelBuilder)
   at System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder()
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
   at EFNS21Tester.EDMXFetchTests.FetchAllCustomersFromUSA() in C:\MyProjects\VS.NET Projects\LLBLGen Pro v5.6\UnitTests\EntityFramework\EFNS21Tester\EDMXFetchTests.cs:line 19

Now this might be to something I have to configure, to copy, to add, but I have no idea what.

Further technical details

EF version: 6.3 on netstandard 2.1 / .netcore 3 Database Provider: SQL Server Operating system: Windows 10 IDE: VS 2019 16.3.0 NUnit, latest

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
FransBoumacommented, Sep 27, 2019

Using \" instead of &quot; worked!

Seriously, this should be in the docs. If it’s there, I couldn’t find it, yesterday (how to pass the connection string to the DbContext ctor). I expected it to ‘just work’ by copying the connection string from the app.config file to the C# code, as it’s up to the ctor to deal with the quotes. Closing this, as it now works.

Are there any docs coming for ef6 with ns2.1 ? As it’s pretty … sparse

0reactions
NikoGJcommented, Feb 9, 2021

I also had the same issue in the same context (mixing .NET Core & EF6), and it was also related with the &quot; I’m glad I found this post as they are quite rare; Upvoting it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework 6 and .NET Core application
In the class library, add EDMX and link it with a SQL database. Refer to the class library in the .NET Core application....
Read more >
Announcing Entity Framework Core 3.0 and ...
We are extremely excited to announce the general availability of EF Core 3.0and EF 6.3 on nuget.org. The final versions of .NET Core...
Read more >
Entity Framework 6 vs Entity Framework Core 3: Comparing ...
To set up this benchmark, I will create two .NET Core 3 class libraries. Each class library will contain a package reference to...
Read more >
Install entity framework core in class library project - YouTube
NET Core Razor Pages Tutorial https://www.youtube.com/playlist?list=PL6n9fhu94yhX6J31qad0wSO1N_rgGbOPV Angular, JavaScript, jQuery, Dot Net ...
Read more >
.NET Core — Using Entity Framework Core in a separate ...
The problem is that the DAL project is a Class Library Project and NOT an ASP.NET Core Application/API. Next, it tries to create...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found