I am a newbie in .Net Core,
I am always using .Net Framework to develop an application (Web App / Web API).
I have develop my custom Framework and it already established. my custom framework use Entity Framework 6.0 (DB First)
Now, I have assigned to develop Web Api in .Net Core. but still use my custom framework to process the data.
on .net core web api I need to assign connection string like web api on .net standard
In .Net Standard my Connection string like below
<connectionStrings>
<add name="CompanyEntities"
connectionString="metadata=res://*/CompanyModel.csdl|res://*/CompanyModel.ssdl|res://*/CompanyModel.msl;provider=System.Data.SqlClient;provider connection string="data source=127.0.0.10;initial catalog=XYZ;persist security info=True;user id=sa;password=zzzxxx123!;multipleactiveresultsets=True;application name=EntityFramework""
providerName="System.Data.EntityClient"/>
for application using .Net Standard Framework (WebApp/ WebApi)… there is no Issue with this. it runs normally
but I don’t know, how I can set connection string on .Net Core? please help
I have tried connection string like this but failed
"ConnectionStrings": {
"CompanyEntities": "Data Source=127.0.0.10;Database=xyz;uid=sa;pwd=zzzxxx123!;MultipleActiveResultSets=true;Integrated Security=False"
}
this is the error :
Severity Code Description Project File Line Suppression State
Error CS0311 The type ‘Classes.Model.Data.Library.CompanyEntities’ cannot be used as type parameter ‘TContext’ in the generic type or method ‘EntityFrameworkServiceCollectionExtensions.AddDbContext(IServiceCollection, Action, ServiceLifetime, ServiceLifetime)’. There is no implicit reference conversion from ‘Clasess.Model.Data.Library.CompanyEntities’ to ‘Microsoft.EntityFrameworkCore.DbContext’.
and this my dbcontext
public partial class CompanyEntities : DbContext
{
public CompanyEntities()
: base("name=CompanyEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
2
Answers
Try this:
Note that you need to install the NuGet package for the database provider you want to access.
For example, if you want to use the MS SQL Server database you need to install
Microsoft.EntityFrameworkCore.SqlServer
NuGet package.You also need to install NuGet package for EF tools
Microsoft.EntityFrameworkCore.Tools
Make sure to add this to your startup.cs:
Modify your dbcontext as follows:
Have a look at this tutorial on EF Core
If you want to use EF 6 (not core) in.NET core, this may be useful but personally I didn’t use this before.
Try this exact below code in your DBContext class:-
Hope now it will resolve your issue.