Link to home
Create AccountLog in
Avatar of deanlee17
deanlee17

asked on

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.
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

In the sample you are looking at, is the author calling Remove() on the something object rather than on anything?
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.
Avatar of deanlee17
deanlee17

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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.
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?
We are using version 4
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.
Hi Fernando

Yes you did, apologies for that. I can get us upgraded to 4.1
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.
Thats perfect!

Thanks for all the help.
Not a problem Dean, glad to help.
Fernando, one last question. How does 'code first' fit into all of this? Is it a third option to ObjectContext and DbContext?
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.
Thanks Fernando, that's cleared it up nicely.
Not a problem Dean, glad to help.