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();
}
@Kellysoares, I advise you to look for the difference between
new Entity()
vsDbSet<Entity>.Create()
, I only use theDbSet<Entity>.Create()
when I need to upload some browsing property through Lazyload.– Tobias Mesquita
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.
– Kelly Soares