Arrow
Arrow

Reputation: 2904

Deleting from Table with Entity Framework/ADO.NET

I am trying to delete a whole row of data from a table. The code I'm using is:

OModel.Cart pncart = database.Carts.FirstOrDefault(
                        pn => pn.OrderId == Session["OOID"] && 
                        pn.PartNumber == Request.QueryString["PartNumber"]);

database.Carts.DeleteObject(pncart);
database.SaveChanges();

And the error it gives me, is:

LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' method, and this method cannot be translated into a store expression. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' method, and this method cannot be translated into a store expression.

It says something about NotSupportException, so I'm guessing you just can't do it this way.

So, how would I go about deleting from the table? I thought this was the correct way.

Upvotes: 0

Views: 313

Answers (2)

jtimperley
jtimperley

Reputation: 2544

I would declare variables for the Session["OOID"] and Request.QueryString["PartNumber"] instead of using them within your linq expression.

I believe it is telling you that it cannot translate those operations.

Upvotes: 4

Anders Abel
Anders Abel

Reputation: 69250

LINQ to Entities get's stuck on embedded expression (LINQ to SQL handles this better). You have to break the embedded expressions out, so that the lambda only contains string constants.

string ooid = Session["OOID"];
string partNo = Request.QueryString["PartNumber"];

OModel.Cart pncart = database.Carts.FirstOrDefault(
                        pn => pn.OrderId == ooid && pn.PartNumber == partNo);

Upvotes: 2

Related Questions