N
N
Nik Faraday2021-11-06 23:56:23
C++ / C#
Nik Faraday, 2021-11-06 23:56:23

How to add entity to database using Entity Framework Core?

Hello!
I had a problem with understanding EF Core in general and its mechanics of adding entities to the database, the question is much deeper than in the question header.

How EF Core entity relationships are built at the C# code level:
For example, I have a Client class with a certain data set and it has an object of the Phone class. These entities are connected by a one-to-one relationship. At the same time, the Phone class object has a foreign key (clientFK) and a reference to it (Client client { get; set; })

If I try to add some entity to the database from the side using the database context, then I need to transfer a new object of the Client class there, respectively, call a constructor on it, which should also initialize the Phone object by calling the constructor to it, inside the Phone object it will also be the associated Client object is initialized again through the constructor, etc...

I mean that, in my understanding, there will be cyclic calls to constructors. I probably don't understand something, please explain!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-11-07
@vabka


I mean that in my understanding, there will be cyclic calls of constructors.

ef has a requirement. So that the constructors were without parameters, as far as I remember.
So there will be no cycle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question