Solved

Linq to delete or remove a record

Posted on 2014-01-06
16
350 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 62

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
 
LVL 62

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 62

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 62

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:deanlee17
ID: 39765445
Hi Fernando

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

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 62

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 62

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 62

Expert Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now