In ASP.NET Core, how to use sqlite - c#

I tried to create a rest api . when I try to change
services.AddDbContext<WebContext>(options =>
options.useSqlite(Configuration.GetConnectionString("WebContext")));
when I try to do this I get this error. I installed sqlite nuget.and also Microsoft.EntityFrameworkCore but I still got the problem
error CS1061: 'DbContextOptionsBuilder' does not contain a definition
for 'UseSqlite' and no extension method 'UseSqlite' accepting a first
argument of type 'DbContextOptionsBuilder' could be found (are you
missing a using directive or an assembly reference?

Try:
using Microsoft.EntityFrameworkCore;
...
services.AddDbContext<WebContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("WebContext")));
...
And you also need to install the Microsoft.EntityFrameworkCore.Sqlite nuget package.

Related

EventFlow registration in .net core 3.1 API

I have package references to EventFlow and EventFlow.AspNetCore. But can't register service:
services.AddEventFlow(ef =>
{
ef.AddDefaults(typeof(Startup).Assembly);
ef.AddAspNetCoreMetadataProviders();
});
Receiving an error:
CS1061 'IServiceCollection' does not contain a definition for 'AddEventFlow' and no accessible extension method 'AddEventFlow' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)
Usings as follows:
using EventFlow.AspNetCore.Extensions;
using EventFlow.AspNetCore.Configuration;
using EventFlow;
using EventFlow.Extensions;
I had same problem. You need to add EventFlow.DependencyInjection package to your project. AddEventFlow method is defined there.

How to fix : 'IDataProtectionBuilder' does not contain a definition for 'PersistKeysToDbContext' and no accessible extension

I am attempting to persist Keys to SQL using the PersistKeysToDbContext extension from services.AddDataProtection(). At the moment we are using PersistKeysToStackExchangeRedis which works really well, however a need was identitied to move it to DB instead.
We are using a .net Core 2.2 project having added the following references:
Microsoft.AspNetCore.DataProtection.EntityFrameworkCore (2.2.0)
Microsoft.AspNetCore.DataProtection.Extensions (2.2.0)
Microsoft.AspNetCore.DataProtection.StackExchangeRedis (2.2.0) (obviously for current functionality)
Microsoft.EntityFrameworkCore (2.2.0)
services.AddDataProtection().PersistKeysToDbContext<ApplicationDbContext>();
I get the following error from the compiler:
'IDataProtectionBuilder' does not contain a definition for 'PersistKeysToDbContext' and no accessible extension method 'PersistKeysToDbContext' accepting a first argument of type 'IDataProtectionBuilder' could be found (are you missing a using directive or an assembly reference?)
1) Make sure you have Microsoft.AspNetCore.DataProtection.Extensions + Microsoft.AspNetCore.DataProtection.EntityFrameworkCore installed (as references).
2) Add using Microsoft.AspNetCore.DataProtection; to Startup.cs (or wherever you have the problem).
I've tested and it works, so if it still doesn't work, you probably need to make sure you haven't missed anything.

How to use IApplicationBuilder and IServiceCollection when downgrading from .NET Core 2.1 to .NET 4.7.1?

I had to change my project from .NET Core 2.1 to .NET 4.7.1 and I fixed almost all errors except the following that are still eluding me
'IApplicationBuilder' does not contain a definition for 'UseHsts' and no extension method 'UseHsts' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
'IApplicationBuilder' does not contain a definition for 'UseAuthentication' and no extension method 'UseAuthentication' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
'IApplicationBuilder' does not contain a definition for 'UseCookiePolicy' and no extension method 'UseCookiePolicy' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
'IApplicationBuilder' does not contain a definition for 'UseHttpsRedirection' and no extension method 'UseHttpsRedirection' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
'IApplicationBuilder' does not contain a definition for 'UseSession' and no extension method 'UseSession' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
'IApplicationBuilder' does not contain a definition for 'UseStaticFiles' and no extension method 'UseStaticFiles' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
'IServiceCollection' does not contain a definition for 'AddAuthentication' and no extension method 'AddAuthentication' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)
'IServiceCollection' does not contain a definition for 'AddSession' and no extension method 'AddSession' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)
I am using Microsoft.AspNetCore.Builder and Microsoft.Extensions.DependencyInjection. What else do I need to use/install/add to get IApplicationBuilder and IServiceCollection to work?
In my WebApi.csproj file I changed the target framework from netcoreapp2.1 to net471.
Old:
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
New:
<PropertyGroup>
<TargetFramework>net471</TargetFramework>
</PropertyGroup>
Following Rik's answer, I searched for more packages and found I had to add all of the following NuGet Packages:
Microsoft.AspNetCore.Authentication
Microsoft.AspNetCore.Session
Microsoft.AspNetCore.HttpsPolicy
Microsoft.AspNetCore.CookiePolicy
Microsoft.AspNetCore.StaticFiles
After I did that I got no more error messages.
The 'AddSession' and 'UseAuthentication' errors can be fixed by using the following nuget packages.
Microsoft.AspNetCore.Session
Microsoft.AspNetCore.Authentication
First of all, switching to 4.7.1 isn't downgrading, it's moving to a different platform. Something you probably don't need to do, unless you want to reuse the code for a Winforms or WPF application.
The Microsoft.Extensions.* packages target .NET Standard 2.0, not just Core, so you can use them in the Full framework as well.
The packages and classes aren't tied to ASP.NET either, except hosting. I'm using them in console applications.
It also means that if your class libraries target .NET Standard 2.0 they can be used by both platforms without changing the target. Perhaps you could move most of the code to .NET Standard 2.0 libraries and leave just the configuration to runtime-specific projects
You don't strictly need hosting to use all the other extensions, although it does provide a convenient API similar to the ASP.NET Core code. You can write your own Startup class with Configure etc methods and call them explicitly. In the end, what you need is access to the IServiceCollection so you can get configured services and run them.
You can add a generic .NET host by using the Microsoft.Extensions.Hosting package. Apart from the common API it adds the ability to host long-running services to non-ASP.NET Core projects.
This blog post shows how you can use the Hosting package to create a console application that starts a long-running service, similar to a Windows service or daemon, eg :
public static async Task Main(string[] args)
{
var hostBuilder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddSingleton<IBusControl>(serviceProvider =>
{
return MassTransit.Bus.Factory.CreateUsingRabbitMq(cfg =>
{
var host = cfg.Host(new Uri("rabbitmq://localhost"), h =>
{
h.Username("guest");
h.Password("guest");
});
});
});
services.AddScoped<IHostedService, MassTransitHostedService>();
});
await hostBuilder.RunConsoleAsync();
}
The .NET Generic Host goes into more detail and shows more examples of logging, DI, configuration etc.
UPDATE
ASP.NET Core is not tied to .NET Core. You can use it in Full Framework projects as well, simply by changing the target runtime in the Project creationg dialog
Try to add "Microsoft.aspNetCore.App" to your *.csproj file ,this should solve the problem .
<ItemGroup>
<FrameworkReference Include="Microsoft.aspNetCore.App" />
</ItemGroup>
Please install Microsoft.AspNetCore.ResponseCaching from nuget package. It can fix your problem. After that described functionality will be available under .net framework.

Error CS1061: 'DbSet<T>' does not contain a definition for 'FromSql' and no extension method 'FromSql' accepting a first argument of type 'DbSet<T>'

I am trying to call view or store procedure using asp.net core 2.1 on mac os webapi.
using System;
using System.Linq;
using Auth.Database;
using Microsoft.EntityFrameworkCore;
public virtual IQueryable<T> ExecuteStoreProcView(string viewProcName)
{
IQueryable<T> queryResult = _entities.Set<T>().FromSql(viewProcName).AsQueryable();
return queryResult;
}
Getting the below error
Error CS1061: 'DbSet' does not contain a definition for 'FromSql'
and no extension method 'FromSql' accepting a first argument of type
'DbSet' could be found (are you missing a using directive or an
assembly reference?) (CS1061)
I am developing webapi using entity framework on mac os.
Research some of the queries in below link :-
Raw SQL Query without DbSet - Entity Framework Core
Raw SQL Query without DbSet - Entity Framework Core
https://forums.asp.net/t/1886501.aspx?System+Data+Entity+DbSet+Entities+User+does+not+contain+a+definition+for+FirstOrDefault+
https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.relationalqueryableextensions.fromsql?view=efcore-2.1
But not able to find the error solution. Can anyone please let me know what I, am missing.
install Microsoft.EntityFrameworkCore.Relational nuget package and then add the using statement to the class
using Microsoft.EntityFrameworkCore;
This might have changed since Umer answered. It is now in the Microsoft.EntityFrameworkCore namespace. But you will need to reference the package.
So...
dotnet add package Microsoft.EntityFrameworkCore.Relational
And add this line...
using Microsoft.EntityFrameworkCore;
In the latest version, Microsoft renamed this method to:
FromSqlInterpolated()
https://learn.microsoft.com/en-us/ef/core/querying/raw-sql
So you'll have to find/replace all the code using .FromSql to .FromSqlInterpolated. They discuss the motivation in the above document, although I have to say the length of the new method name is unwelcome.
What the other answers say is still necessary - you'll need to have installed the Nuget package Microsoft.EntityFrameworkCore.Relational, and, have added a using statement for Microsoft.EntityFramework.
It solved for me with this using this line:
using Microsoft.EntityFrameworkCore;
Hope this might be useful as it's related to the above issue and is likely that others will have the same problem. I was not able to access .FromSql inside of a service that resided in a seperate service project from the Web.Api (MVC Project) I was able to access the .FromSql from within a controller in the Web.Api project. What confused me was that there is no direct reference in the web.api project for Microsoft.EntityFrameworkCore.Relational The Microsoft.EntityFrameworkCore.SqlServer nuget package is referenced by the EntityFramework project. By adding a reference to the service project the services are now able to use the .FromSql. It's been a long day but I still don't know why the web.api works but the service project needs a direct reference.
In Net core 6, the method name has changed to FromSqlRaw.
So, you need to install the bellow package if you haven't already:
PM > Install-Package Microsoft.EntityFrameworkCore.Relational
and also use FromSqlRaw instead:
using Microsoft.Data.SqlClient; //parameters must come from this namespace and not System.Data.SqlClient
var param1Value= new SqlParameter("Param1", "value1");
context.SomeEntity
.FromSqlRaw("EXECUTE dbo.StoredProcedureName #Param1", param1Value)
.ToList();
It solved for me with this using this line:
$ Using System.Linq;
Add this line on top of the file where you are getting this error. FromSql is an extension method and is implemented in Microsoft.EntityFrameworkCore.Relational assembly.
using Microsoft.EntityFrameworkCore.Relational;

ScriptBundle Reference missing

I am building a MVC 3 application. I just added a ADO.NET entity data model.
When i try to build the solution, it gives the following error
The type or namespace name 'ScriptBundle' could not be found (are you missing a using directive or an assembly reference)
I already have reference to Microsoft.AspNet.Web.Optimization.
Please help...
Did you installed it from NuGet package manager? I think that is the reason you are facing the issue
Install-Package Microsoft.AspNet.Web.Optimization

Categories