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:
```c#
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(IDictionary2 parsetable, String connectionString, IList1 validKeywords)
at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions..ctor(String connectionString, IList1 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.InternalSet1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.WhereTSource
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:
```cs
[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="data source=nerd.sd.local;initial catalog=Northwind;integrated security=SSPI;persist security info=False"" 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' :D
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.
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
Are you using xUnit.net? We had to use a pretty ugly workaround to get App.config to work...
I think the " in the C# string is wrong. See Connection Strings in ADO.NET for the different ways to de-tangle quotes
No I use nunit.
The connection string I use is exactly the same in app.config. Not sure why it works there or not, but I'll retry tomorrow with different quotes. It's a bizarre error indeed, so it's likely something stupidly simple 馃槅
The App.config issue probably extends to all test runners. See (and upvote) https://github.com/dotnet/corefx/issues/32095 (the title doesn't reflect the entire issue)
Using \" instead of \" 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
Are there any docs coming for ef6 with ns2.1 ? As it's pretty ... sparse
We have a few items on our TODO list. Please let us know if you find any other areas related to .NET Standard that you think need more guidance.
I also had the same issue in the same context (mixing .NET Core & EF6), and it was also related with the \" I'm glad I found this post as they are quite rare; Upvoting it.
Most helpful comment
Using \" instead of \" 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