EF 9
Hi, I am learning C#, EF, gRPC with a small project (A Blog).
I am using SQLServer as DB.
I have a problem with EF, I want to validate that the user id sent to me really exists so I can't create a post with an id of a user that doesn't exist.


29 Replies

The problem I have is that at the moment of generating the migration of
Post
the migration generates in the same file the User
table (this table is already created by the other microservice) and at the moment of executing it, it is broken.
Any idea how I can solve it?If user is not null, it means the user exists
Ah, microservices... assuming you use a single database, you should have a single project for all your database models and dbcontext
Then reference that in each service, or wherever needed
I don't understand
Isn't each microservice an independent project?
Yeah
But the database is its own thing, that backs them all
Say you were not using EF, you'd have some
.sql
file describing your database structure or something
That would be your Data
project
Just a config for the database, basicallyAnd that's not what EF is good for?
It is
So my project structure is wrong?
The database entities describe the rows, the context describes the database
The project is your equivalent of an
.sql
file that would also contain all that
You would not have partial .sql
files in each microservice, each describing 2-3 tables of it, would you?
That said, I never used microservices, nor do I intend to touch them with a ten-foot pole unless I have a gun to my head and a multibillion contract
So perhaps there are ways to scatter your EF config across multiple projects that I don't know ofI said the same thing, but I got the price
But, generally, EF treats each
DbContext
as a separate databaseOKay, then I have to find a way to use EF globally?
A separate project
This separate project can be my REST API for the JS client?
What?
No
Just a class library
Pff I don't understand anything bro, I'm JS dev but I changed my position and now I have to use C#.
Bad idea to jump into a project this complex with no knowledge of ASP and C#
This is not yet a job, it is a learning project so as not to arrive at work at 0 in January.
Separate projects for each service
Each project being a WebAPI or whatever
Then, a separate
Data
project for just the database models and the dbcontext
A class library
Referenced by all the services
Then, run migrations on that projectOh okay, so what I have to do is to remove the models and migrations from each gRPC project and put them in a new one called idk
PrepareDB
that project is going to take care of accommodating all the tables.
Then I can run each microservice without any problem.Yeah
Oh, and I can't put this in my api rest?
Because these microservices are in gRPC and to connect them to the client (Angular) I will use a REST api.
But where is the part where microservices communicate with each other?
Well, microservices communicate with eachother using... something
gRPC, REST, whatever
There's no, like,
Communicator
project or whatever
Each microservice is an API that calls other APIs, basically
If you need to get a, say, blogpost
You query the blogpost service, that queries the user service to get author data, and the tag service to get the list of tags
One API call turns into three
Alternatively, the frontend queries all three services
Post service for the post data, user service with the post ID for the author data, and tag service with the post ID for the list of tagsBut for example, I'm going to create a blog,
I have 2 gRPC projects
- UserService
- PostService
I will have one more project to set up the DB
- PrepareDB
This PrepareDB will be referenced in the 2 gRPC projects.
But how do these 2 gRPC projects communicate with each other? Simply with the
Both
so that they are grpc clients and servers?
Although looking at it, I believe that they do not have to communicate between them.
Then I will have my REST api where I will also reference that prepareDB project, and here it will also be a gRPC client.
To be able to query the microservices
And then transform it to REST endopointIf they need to communicate with eachother, they can do that through gRPC, REST, whatever
Well, if the REST api is only supposed to be the glue to the microservices, then it should not need to reference the database project
Not to mention, having a glue API like that kinda defeats the point of microservices
Well, for starters, microservices are not an architectural solution, they're an organizational one
But, by design, you should be able to edit any microservice at any time without impacting anything else
If you change what a given service returns... the API will have to change to account for it
Okay, I think I understand how to solve it, thank you very much!