Executing raw SQL using EntityFrameworkCore and SQLite on .NET Core - entity-framework

I am working on some security illustrations and therefore need to purposefully write insecure SQL queries.
The environment I am looking at is .NET Core (not full .NET), the driver used is EntityFrameworkCore and the database type is SQLite.
Regular connections using the built in lookup and query functions work without any issue. However there seems to be no function that would allow me to execute a raw SQL statement.
Several online resources recommend using the .FromSql() and .SqlQuery() methods on the database / context, but neither of these methods seems to exist in the .NET Core driver.
Is there any way to execute a raw SQL query in this setting?

As pointed at comment:
Check if there is Microsoft.EntityFrameworkCore.Relational reference on your project references(if you use sqllite it should be in the Microsoft.EntityFrameworkCore.Sqlite reference).

You could use the Nuget Package:
Microsoft.EntityFrameworkCore.Sqlite
and
db.Database.ExecuteSqlCommand("YOUR SQLITE SQL COMMAND");
where db is an instance of a class derived from DbContext

Related

How to combine CosmosDB, Entity Framework and OData in .Net Core 6.0?

I have an application that uses Entity Framework, OData and SqlServer in a .Net core 3.1 scenario and it works great. Ultimately I want to use Cosmos DB on Azure in lieu of SQL Server.
While I can get the app to connect to Cosmos the application fails in calls to FromSqlRaw which the 3.1 compatible libraries do not support.
I've tried upgrading the project entirely to .Net core 6.0 and while this compiles without error I get problems loading assemblies using a package called MediatR (for injection) when the application starts.
My question is twofold: Do the .Net 6.0 compatible libraries for Cosmos DB correctly handle FromSqlRaw and/or is there a workaround for the problem if they do not.
If you are asking if you can send T-SQL or ANSI-SQL to FromRawSQL in EF with the Cosmos DB provider the answer is no.
Cosmos DB is a NoSQL database. The FromRawSQL is intended to allow users to send Cosmos DB's own dialect of SQL and was put in as a way to make the Cosmos provider higher compatible where the provider maybe lacked specific capabilities exposed by Cosmos DB itself.

What is a good action plan for database first connect data from postgresql to a c# project with dot net core and using entity framework core

I need to successfully do database first connect data from postgresql to a c# project with dot net core and using entity framework core.
Am familiar with mvc with sql and entity framework full database first (in this case we use the Entity Data Model Wizard which makes things easier.
I am not familiar with EFcore, dot net Core and also postgresql.
I successfully did a code first connect data from postgresql to a c# project with dot net core and using entity framework core, and using npgsql, on a console app.
Now I need to do database first web application, and should I try to edit my existing code first console app to try database first? Or should I build a new mvc project from scratch?
Also, if I do from scratch, what will be a good sequence I try
eg 1. try entity framework core tutorial first (which uses sql and is only code first),
then2. try to see how to do database first using reverse engineering
then3. try to replace the sql with postgresql
or are there any other methods that are better?
Scaffolding works with postresql.
Create project which will have your database entities and context. Install efcore tools and npgsql for this project.
Then, inside new project try this command using cmd:
dotnet ef dbcontext scaffold "Host=localhost;Database=databaseName;Username=user;Password=password" Npgsql.EntityFrameworkCore.PostgreSQL -t table1 -t table2 -o outputFolder
You dont have to pass -t parameters if you want to scaffold whole database.
If some error happens, try --force argument.
You should be able to use this database via context created by ef core.

MongoDbContext for data migration in EF core?

Currently I'm trying to find a method to use EF core 2.2 for data migration. After reading the instruction at:
https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/
I realise that it only support DbContext.
At the moment, in my project I have mongoDBContext and IMongoDBContext for my database and I couldn't find any package that support them. I also try using Microsoft.EntityFrameworkCore.Design but it's not helping. Is there any solution for me to use mongoDbContext as the database provider, or any packages that support mongoDB as the database provider?
As for my attempt to create a migration file despite not having DbContext:
From my terminal
dotnet ef migrations add InitialCreate
Error
No DbContext was found in assembly '.Data'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
Thank you so much!
MongoDb is schema-less storage.
Entityframework Migrations is use to update schema of the tables. Yes migrations can also run update/delete/insert part of migration but only because Sql is dependent on schema.
In addition EF is not supporting MongoDb since mongo db is not using sql, mongo db has his own api/language.
Yes recent updates of EFCore supports CosmosDb but only because CosmosDB can talk sql
To sum up.
If you need to modify schema probably your MongoDb design is wrong.
If you want to talk to MongoDb use https://docs.mongodb.com/ecosystem/drivers/csharp/
If you are not familiar with MongoDb use what you familiar with
Yes, Mongodb is shema-less storage. But a solution always has some sort of schema in your POCO/DDD typed/dynamic objects and related domain logic. And you may change these contracts over time. So with Mongo we have the ability to perform two types of migrations: upgrade scripts ( like with RDBMS) or on-the-fly document migration when documents are used. You definitely can use your MongoDB without changes in existing documents in projects like collections of metrics and IoT device data or highly dynamic objects but this is not always the case.

How to set ProviderManifestToken for Entity Framework using Code First?

We use EF 4.1 and code first. While our production databases are SQL Server 2005, some of our developers have SQL Server 2008 locally. Our TFS Build server also is 2008. When running unit tests locally and on the build server we run into issues because of the differences between datetime and datetime2. Even though the column on the database is "datetime", using SQL Profiler we can see that EF treats the column as "datetime2".
I saw articles mentioning that we should set ProviderManifestToken in our EDMX file to 2005 to solve this problem but we're using EF code first. Is there not a way so solve this using code first?
For EF5 Code First it's a bit more than just changing a string in the xml. Take a look at this blog post. It contains an example of setting the provider manifest token explicitly. In EF6 you can set just configure a resolver that returns provider manifest token.
see this
Just use datetime instead of datetime2. It works in my project.

What's a good local database with Entity Framework

What would be a good choice for a local database to use with Entity Framework for an WPF application that needs local database.
I dont want to use MSSQL Server cos that'd be an overkill and or MSSQL CE cos few days back I was using SQL CE 3.5 and had all sorts of issues of it not supporting server generated IDs.
I've read MS Access has issues as well. and dont want to use SQLite either.
TIA
SQL Server Express sounds a good fit to me.
What about MySQL Embedded?
You can use either EF provider by Devart (it is a part of dotConnect for MySQL Professional), or the provider integrated into MySQL Connector /NET.