evgomes / supermarket-api

Simple RESTful API built with ASP.NET Core and .NET 8 to show how to create RESTful services using a decoupled, maintainable architecture.

Home Page:https://medium.com/free-code-camp/an-awesome-guide-on-how-to-build-restful-apis-with-asp-net-core-87b818123e28

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UnitOfWork not saving data in my db

nunomarques97 opened this issue · comments

Hello!!

I made an api using your tutorial
i can post objects into the database. However when i restart the app I loose all data. I follow ur pattern of UnitOfWork. Not sure if my EFCore version matters but im using 3.1.8 and Im using netcore 3.1. Also, i made the base project in netcore 2.2 like u did and then migrate to 3.1. any ideas what could be with the migration from 2.2 to 3.1?

Hi @nunomarques97

Which database are you using? Which database provider are you using with Entity Core?
I used an in-memory provider for this tutorial, so all data is kept in memory. Once you restart the API, all data is lost.

Here is an example of how to configure Entity Framework to store data into SQL Server, so data is not lost after API restarts: https://entityframeworkcore.com/knowledge-base/54460412/how-to-connect-configure-entity-framework-to-connect-to-sql-server-

Can you confirm which provider you are using? If you're already using a different database provider, please, share a bit of your code, this way I can check what is happening.

Thanks.

image

here i my .csproject package does it help? i also use in memory provider

Hi @nunomarques97

Sorry for the late reply.

It seems you have installed the SQL Server provider, but you are still using the In-Memory provider to persist data.
Try changing the AddDbContext directive in the Startup class like this:

services.AddDbContext<YourDbContextClass>(opt =>
{
	opt.UseSqlServer("your_connection_string_here");
});

You can find more information on how to configure Entity to use SQL Server and how to add the connection string here: https://docs.microsoft.com/pt-br/ef/core/dbcontext-configuration/

Let me know if it works.

Hi,

I'll close this due to inactivity. If there are problems yet, please, let me know.

Thank you.