I am having a problem with a 1 to 1 relationship, it is more or less like this A Car has a Driver, in this case in the model I have
public class Auto {
int IdAuto
string Modelo
int idConductor
Conductor Conductor
}
Now, so far everything is fine, now what happens is that this driver has a username and password and I have to log in, and when I log in and bring the Driver, I want to know what car is assigned to him.
public class Conductor {
int IdConductor
string nombre
int IdAuto
Auto Auto
string usuario
string password
}
This does not allow me to generate the migration file, of course, because there is a circular relationship, if I bring the car, the driver brings me, who in turn has a car that has a driver, etc.
Is there any way, with annotations, to make this relationship and tell it to cut on the first load and not cycle it? Thanks a lot
This type of situation can be solved in the following way. I clarify that the example that I am going to show you is with a test ASP project but in the end it is the same Entity Framework, so it can be applied perfectly in your case.
To define a 1 to 1 relationship with EF you can define in the Car class that its Id is going to be the same Id as the Driver's, in this way you have defined a 1 to 1 example:
With this annotation you are declaring that the Car Id is going to be the same as the Driver Id, so when you fill in the DriverId field in your Car form you are establishing its key at the same time. After doing a migration you can see in your DB the auto table the fields it has and you will see how everything is fulfilled. I hope it helps