How to use the Foreign Key annotation in C#?

Asked

Viewed 687 times

5

I am layman and would love to learn about.

I will use in the example Model A and B not to get confused:

I saw about the Model A take note Foreign Key in prop virtual Model B virModelB referencing a prop int ModelBID to store the data.

And in the Model B have a prop virtual ICollection<ModelA> virModelA, but I don’t understand how one Model is "seeing" the other... There is some difference in making a Model for each relationship (one-to-one, one-to-Many, Many-to-Many)?

In short:

  • Build each Model
    • Foreign Key Annotation
    • Icollection<>
  • Relationships
    • One-to-one
    • One-to-Many
    • Many-to-Many
  • @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... :)

  • 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

  • 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)

1 answer

5


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 AutorIdin 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.

  • Thank you so much!! Just one last abuse in your time, could you show me the Many-to-Many? It’s one of my biggest problems...

  • take a look at this link @Leohenrique

  • I tried to make the link example, but this item DbModelBuilder, It is not ever found by my VS, I do not know if it is because my VS is 2013 and so it has not the necessary Assembly... But td well, I did not know this site that several of you sent me and I liked it a lot, now it’s just me deepen... Thanks to all involved!

  • @Leohenrique, is probably missing some reference in your application not to recognize Dbmodelbuilder, because it is a member of Dbcontext. Or it could be the version you’re using, you can tell if it’s EF Core, or if it’s Entityframework 6 ?

  • use the Entityframework 6.2.0

  • So, the link that João Paulo posted is from EF Core, the link to Entityframework 6 is this: http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx. but that’s not the problem, as both versions use the same object (Dbmodelbuilder), your application is probably without the System.Data.Entity reference or some other reference required for the Entityframework

  • I’m wearing this System.Data.Entity, same because I use the DbContext, I think there’s some other reference missing, but I don’t know which one...

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.