Basically the DataAnnotations [ForeignKey]
is only to clarify the foreign key field in relationships between two entities, for example:
public class Livro
{
public int Id { get; set; }
public string Titulo { get; set; }
public int AutorId { get; set; }
[ForeignKey("AutorId")]
public virtual Autor { get; set; }
}
public class Autor
{
public int Id { get; set; }
public string Nome { get; set; }
public virtual ICollection<Livro> Livros { get; set; }
}
In the example quoted above we have a Book class and an Author class, this is a One-To-Many relationship of Autor
for Livro
, because an author can have several books, plus one book has only one author. We use the annotation [ForeignKey("AutorId")]
to explain to Efcore that our foreign key that references the book to the author is AutorId
. We could also do the relationship without explaining to Efcore which field will be the key, causing EF Core itself to create an automatically generated field to perform the key, in this case we would not even need to create field AutorId
in the class Book as below:
public class Livro
{
public int Id { get; set; }
public string Titulo { get; set; }
public virtual Autor { get; set; }
}
public class Autor
{
public int Id { get; set; }
public string Nome { get; set; }
public virtual ICollection<Livro> Livros { get; set; }
}
The two examples above depict the same class structure, the difference being that in the second example who will generate the foreign key is EF Core itself. Which will generate the following table structure (I will use SQL as the basis):
CREATE TABLE Livro(
Id INT NOT NULL,
Nome NVARCHAR,
PRIMARY KEY(Id)
);
CREATE TABLE Autor(
Id INT NOT NULL,
Titulo NVARCHAR,
AutorId INT NOT NULL,
FOREIGN KEY (AutorId) REFERENCES Livro (Id)
PRIMARY KEY(Id)
);
OBS.: It may be that the structure of the tables has some difference as to this, more must approach to the described above.
Efcore promotes the use of fluent mapping without the use of Dataannotations, so in the official documentation of Microsoft or of Efcore, probably you will not find reference to the use of Dataannotations. However you will find more details of the use of Dataannotations in Entityframework 6 Code First documentation.
@Joãopauloamorim Yes, I do it differently and I started studying it because I think it gets more organized and so... So I wanted an example with explanations to have a model to cure my doubts. 'Cause the ones I’ve been seeing online, there’s always some lingering doubt and I believe that here, you can help me... :)
– LeoHenrique
I believe that this link can help you a lot in various things about the Entity Framework. Any questions that arise please contact us. Right now it’s kind of hard to come up with a good answer for you, but if no one answers, I’ll draw it up and post it here later. http://www.entityframeworktutorial.net/efcore/configure-one-to-many-relationship-using-fluent-api-in-ef-core.aspx
– Pedro Paulo
Thank you so much @Pedropaulo! I created a post about the problem that I was having, but I saw that no one could help me and that’s when I decided to create this, being more theoretical, to expand my knowledge so that I can alone solve my problem. (https://stackoverflow.com/questions/54793570/models-join-asp-net-mvc)
– LeoHenrique