📜 ⬆️ ⬇️

The new version of the Entity Framework will support foreign keys in the conceptual model.

Many who now work with the Entity Framework complain about the lack of foreign keys in the conceptual model, that is, in those classes that the Entity Framework generates. As a result, the simple task of adding or updating a record often doesn’t look very elegant. That is, you either have to load a linked record, which leads to the execution of an extra database query, or create a new object of the EntityKey class, which requires writing extra code and, after a simple and easy-to-use LINQ to SQL, looks at least strange.



Fortunately, the Entity Framework team has taken into account these wishes and in the new version it will be possible to choose when generating a conceptual model whether to add foreign keys to it or not. As a result, updating the records will look quite simple and clear:
')
using (var context = new Context())
{
//Create a product and a relationship to a known category by ID
Product p = new Product
{
ID = 1,
Name = "Bovril",
CategoryID = 13
};
//Add the product (and create the relationship by FK value)
context.Products.AddObject(p);
context.SaveChanges();
}


Details about this can be found in the Entity Framework team blog.

Source: https://habr.com/ru/post/54940/


All Articles