Solved

Linq to delete or remove a record

Posted on 2014-01-06
16
354 Views
Last Modified: 2016-02-10
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.
0
Comment
Question by:deanlee17
  • 8
  • 7
16 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39758840
In the sample you are looking at, is the author calling Remove() on the something object rather than on anything?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39759340
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
 

Author Comment

by:deanlee17
ID: 39761437
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39762974
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
 

Author Comment

by:deanlee17
ID: 39763023
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
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39763126
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
 

Author Comment

by:deanlee17
ID: 39765299
We are using version 4
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39765395
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
 

Author Comment

by:deanlee17
ID: 39765445
Hi Fernando

Yes you did, apologies for that. I can get us upgraded to 4.1
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39765684
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
 

Author Comment

by:deanlee17
ID: 39766207
Thats perfect!

Thanks for all the help.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39766600
Not a problem Dean, glad to help.
0
 

Author Comment

by:deanlee17
ID: 39767311
Fernando, one last question. How does 'code first' fit into all of this? Is it a third option to ObjectContext and DbContext?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39768385
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
 

Author Comment

by:deanlee17
ID: 39769194
Thanks Fernando, that's cleared it up nicely.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39769366
Not a problem Dean, glad to help.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question