Questions with Entity Framework (Navigation Properties and Create method)

1

I have been reading several articles and so far I have not been able to understand the real function of navigation property. In the last articles I read, it said it serves as a foreign key for navigation, but when I tried to create a project starting with Model First, the foreign keys did not have the same name as the navigational properties. Another question is about the Create method, in my opinion, this method would have the function of creating the 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 anyone clarify these two doubts 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();
}
    
asked by anonymous 08.09.2015 / 19:08

1 answer

2

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

There are two functions of the navigation properties:

  • Tell Entity Framework a relationship between entities;
  • Have the Entity Framework automatically fill in 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 like this:

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

    public virtual Categoria Categoria { get; set; }
}

The Entity Framework would map Categoria to Produto in the same way. 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 be rather a problem, depending on how the system is developed.

In the last articles I read, it said it serves as a foreign key for navigation, but when I tried to create a project starting with Model First, the foreign keys were not given the same name as the navigation properties. h3>

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

Another question is about the Create method, in my opinion, this method would have the function of creating the 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. / h2>

That's not 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 (or added) to the context.

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

    

08.09.2015 / 20:28