Domanda

Regarding the topic, some entities have multiplicity relationship

I learned all of these in my studies, however, is it necessary to maintain in c# programming such as using Entity framework in this example ? :

http://www.asp.net/web-forms/tutorials/getting-started-with-ef/the-entity-framework-and-aspnet-getting-started-part-1

this is because I am confused on how the updating of entities works, such as cascading and why it's important.

I find it easier to just code the entity being independent with each other despite the fact they are actually related in real life such as category-product.

can anyone briefly show how CRUD works with this relationship in questions?

È stato utile?

Soluzione

Entity Framework with its entities and their associations between each others gives you the advantage that the framework itself - the OR - Mapper - takes care of all the updates, deletes and inserts for you. It takes care of maintaining the relationships between the entities and the entities itself. You 'just' need to define the model.

You can always go the manual road and code all that yourself. But in most cases defining the entities with their properties and relations between them and having Entity Framework or any other OR-Mapper (like nHibernater) take care of all that, is a huge plus in terms of development speed, maintainability and consistency.

If you define your entities separate from each other - with other words, you do not work with associations between them - you can not navigate from one object to the other. See the example:

Example a) with associations defined:

2 entities: order and orderitem One order can have up to n order items. You can traverse/navigate from the specific order to all associated order items, e.g.

var myOrder = Repository.FindOrderById(15);
var orderItems = myorder.OrderItems;

Example b) without associations defined:

2 entities: order and orderitem Without associations defined, you can not navigate from one entity to another. You would need to query for all order items separately, e.g.

var myOrder = Respository.FindOrderById(15);
var orderItems = Repository.FindOrderItemsByOrder(15);

This is not saying how the entities are loaded. In example b, to query the order and the related order items, the OR-Mapper executes 2 queries against the database. In example a, you could define whether to lazy load the entities in 2 queries or to eager load in 1 query. Having defined associations give you the option to choose whether to load related entities eagerly or lazy.

This is a good starting point for Entity Framework in general: http://msdn.microsoft.com/en-us/library/bb386876.aspx

That is a good overview how relationships between entities work and what options you have: http://msdn.microsoft.com/en-us/library/ee373856.aspx

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top