Questions with Entity Framework (Navigation Properties and Create method)

Asked

Viewed 348 times

1

I have been reading several articles and so far I could not understand the real function of the navigation property. In the last articles I read, it said that it serves as a foreign key for navigation, but when I tried to create a project starting with the Model First (Model First), the foreign keys did not have the same name as the navigation properties. Another question is about the Create method, in my view, this method would have the function of creating entities if they do not exist in the database, but when I tried to delete one of the entities and test the code, this did not happen. Could someone clear up these two questions I still have? Follow the code with the Create method below:

using (var db = new AccountingSystemContainer())
{
    var invHeader = db.InvoiceHeaderSet.**Create();**
    var invDetail = db.InvoiceDetailSet.**Create();**

    invHeader.Total = 150m;

    invDetail.ItemDescription = "Algum Item";
    invDetail.Price = 75m;
    invDetail.Quantity = 2;

    invHeader.InvoiceDetail.Add(invDetail);

    db.InvoiceHeaderSet.Add(invHeader);
    db.SaveChanges();
}

1 answer

2


I have been reading several articles and so far I could not understand the real function of the navigation property.

Two functions of the navigation properties:

  • Indicate to the Entity Framework a relationship between entities;
  • Make Entity Framework automatically populate data from one (or more) related entities.

For example, suppose the following:

public class Produto {
    [Key]
    public int ProdutoId { get; set; }
    public int Categoria Id { get; set; }

    public virtual Categoria Categoria { get; set; }
}

I could make this relationship so:

public class Produto 
{
    [Key]
    public int ProdutoId { get; set; }

    public virtual Categoria Categoria { get; set; }
}

The Entity Framework would map Categoria with Produto likewise. What changes is that the Entity Framework chooses how the foreign key will be called, and that it is not exactly accessible within the application, which can rather be a problem depending on how the system is developed.

In the last articles I read, it said that it serves as a foreign key for navigation, but when I tried to create a project starting with the Model First (Model First), the foreign keys did not get the same name as the navigation properties.

Yes, this is not guaranteed precisely because you can have defined the entities without specifying the name of the keys.

Another question is about the Create method, in my view, this method would have the function of creating entities if they do not exist in the database, but when I tried to delete one of the entities and test the code, this did not happen.

That’s not exactly how Create works. According to the documentation:

    // Summary:
    //     Creates a new instance of an entity for the type of this set.  Note that
    //     this instance is NOT added or attached to the set.  The instance returned
    //     will be a proxy if the underlying context is configured to create proxies
    //     and the entity type meets the requirements for creating a proxy.
    //
    // Returns:
    //     The entity instance, which may be a proxy.

That is, the created instance is not necessarily added (Added) or attached (Attached) in context.

As for the deletion part, I need the code used to perform the deletion so I can improve the response.

  • 1

    @Kellysoares, I advise you to look for the difference between new Entity() vs DbSet<Entity>.Create(), I only use the DbSet<Entity>.Create() when I need to upload some browsing property through Lazyload.

  • Toby, thanks for the suggestion, I’ll look into it. Gypsy, I will open a new question and post the code for you to explain to me the exclusion and functioning of the navigation property in it, because in the created model it takes the name of a field and determines as foreign key without using the name of the navigation property.

Browser other questions tagged

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