why use collection to persist a single instanse?

Feb 5, 2011 at 12:26 PM

I think this is a great tool that will increase productivity.

I am currently learning the tool.

Out of curiosity :

Why do i have to create a collection instanse to persist a single object instanse ?

Would'nt it be easier o simply declare a new object instanse , set its properties and write Object.Persist., without the need to use its  collection class

Giannis

 

 

 

Coordinator
Feb 8, 2011 at 4:30 PM

There are two data access layers. In the Entity Framework (EFDAL) this is just standard EF. The pattern is whatever Microsoft dictated. However I assume you are talking about the NHDAL. The generated assembly used ADO.NET in the background as its access layer. It is built on the factory pattern so you must get an object from a parent collection. Now that said you can call an object's "Persist" method and it will singly be saved back to the database. This only works if you are making changes to a single object and there are no related objects changed of course. So in answer to your comment you can actually save an object by calling the Object.Persist method. You can even load an object without the collection as well. There is a static SelectByPK method on each object that will return a single object. Of course if you wish to use LINQ to perform a complex query you will need to call this on the collection.

In your question above you were asking about adding an object. In this scenario you have to create the object from the collection because the whole framework is based on the factory pattern. However there is a Code First beta generator out. If you use the EF code first generator to create your code you can do pretty much what you said. The generated objects are POCOs so you can create one and set its properties. However you will have to add it to the context to save it.