Linq to delete or remove a record

Hi guys,

I was following Julia Lermans book on DbContext, she uses the remove command, but I cant. So this works fine for me and the record is removed from the database

 using (var anything = new AscentEntities())
               {
                   var something = (from d in anything.Qte_Lines where d.LinesID == 49 select d).Single();
                   anything.Qte_Lines.DeleteObject(something);
                   anything.SaveChanges();
               }

Open in new window


but if I change .DeleteObject to .Remove I get an error. She seeme to use the remove command with no problems in the examples.

Thanks,
Dean.
deanlee17Asked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi deanlee17;

To your question, "So what is the difference between the two?", When Entity Framework was first released the only data context was ObjectContext which is a heavyweight object containing many functionalities while the DbContext is a lighter weight object containing only the most use functions found in the ObjectContext. The DbContext was introduced into Entity Framework in version 4.1 where Code First was introduced and therefor if you use Code First approach you need to use the newer DbContext.

Using this line of code
using (var anything = new AscentEntities())

Open in new window


Instantiate the data context for the Entity Framework model so depending on which data context you have configured ObjectContext , the old way, or DbContext, the new way, that line of code will create one of them.

To determine which type of Data context is being created go to the Solution Explorer and open the node with the EDMX extension and look for a file that has the ModelName.Context.cs and see which class it inherits from ObjectContext or DbContext.
0
 
Carl TawnSystems and Integration DeveloperCommented:
In the sample you are looking at, is the author calling Remove() on the something object rather than on anything?
0
 
Fernando SotoRetiredCommented:
Hi deanlee17;

It sounds like to me that you have a reference to a ObjectContext and not a DbContext seeming DbContext.DbSet does not have a DeleteObject but only a Remove. Could you verify that and also please post the exception you are getting and inner exception.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
deanlee17Author Commented:
Ahhhh ok, this could be exposing my lack of knowledge with entity framework. I think you are correct. So what is the difference between the two? In my example I was assuming

using (var anything = new AscentEntities())

was setting up the link to DbContext, as AscentEntities is obv the name of my entity framework. Clearly ive confused things.
0
 
deanlee17Author Commented:
Hi Fernando,

Thats a perfect explanation, thanks a lot. I did as you advised and it is indeed ObjectContext, which im assuming my application setup by default when I added the entity framework. What would be the easiest way to update to update it to DbContext or is it a little late now?

Many Thanks.
0
 
Fernando SotoRetiredCommented:
Hi Dean;

It is not late. The procedure to convert to DbContext depends on which version of Entity Framework you are using, So what version are you using?
0
 
deanlee17Author Commented:
We are using version 4
0
 
Fernando SotoRetiredCommented:
Hi Dean;

As I stated earlier, "DbContext was introduced into Entity Framework in version 4.1 where Code First was introduced", because you have EF 4 it is not possible to use DbContext in this project until you upgrade to the .Net Framework 4.1 or above.
0
 
deanlee17Author Commented:
Hi Fernando

Yes you did, apologies for that. I can get us upgraded to 4.1
0
 
Fernando SotoRetiredCommented:
Hi Dean;

OK then, when you upgrade to .Net 4.1 to the project I do not know if they will ask you if you wish to update the ObjectContext to a DbContext or not but if they do then the update should take care of everything for you. If it does not then you can always do the following.

1.

First before starting backup your project in case something went wrong

2.

In Solution Explorer open the tree node for the EF model.

3.

Underneath the above node delete the node ModelName.Designer.cs, it has all the entities and ObjectContext class code

4.

Open the EF Designer and right click on an empty space in the design surface and select Add Code Generation Item…

5.

Select ‘Code’ from the left menu

6.

Select ‘ADO.NET DbContext Generator

7.

Name the item ‘YourModelName.tt’

8.

Click ‘Add’

9.

Save All and then Rebuild project to generate the DbContext and Entity classesAt this point you should be good to go.
0
 
deanlee17Author Commented:
Thats perfect!

Thanks for all the help.
0
 
Fernando SotoRetiredCommented:
Not a problem Dean, glad to help.
0
 
deanlee17Author Commented:
Fernando, one last question. How does 'code first' fit into all of this? Is it a third option to ObjectContext and DbContext?
0
 
Fernando SotoRetiredCommented:
In building an Entity Framework model there are three approaches. One is you create a database in DBMS and then create an EF model from that database, this is called Database First. Two you create a model in the EF designer by placing tables, columns and relationships and building the Database from that EF model, which is called Model First and third you create classes in code that represents tables and columns in the database and using configurations commands make the relationships between tables and when you execute the code if it can not find a database with that design it will create one in the database and this is called Code First. Database First and Model First can be used with either ObjectContext or DbContext. Code First can only be used with DbContext because DbContext added many features that is needed to support Code First.
0
 
deanlee17Author Commented:
Thanks Fernando, that's cleared it up nicely.
0
 
Fernando SotoRetiredCommented:
Not a problem Dean, glad to help.
0
All Courses

From novice to tech pro — start learning today.