Solved

How do I write the code to delete a table row in my db from my MVC controller? (ASP.NET, MVC)

Posted on 2016-10-25
7
68 Views
Last Modified: 2016-10-26
I'm using the Entity framework in my MVC project. I would like to delete a record when the user navigates to a specific controller. What is wrong with my code below? And how can I accomplish this?

The table name is: fooItem

MYENTITIES oe = new MYENTITIES();
fooItem j = oe.fooItems.ToList().Where(d => d.fooItemId == Id).First();

oe.fooItems.Remove(j);
oe.SaveChanges();

Open in new window

0
Comment
Question by:bmanmike39
7 Comments
 
LVL 14

Assisted Solution

by:Lokesh B R
Lokesh B R earned 250 total points
ID: 41859828
Hi,

try the below code

fooItem f;

using (var ctx = new MYENTITIES())
{
    f = ctx.fooItems.Where(s => s.fooItemId == Id).FirstOrDefault();
}

if(f != null)
{
   using (var newContext = new MYENTITIES())
   {
        newContext.Entry(f).State = System.Data.Entity.EntityState.Deleted;    

        newContext.SaveChanges();
    }
}

Open in new window

0
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 41859920
SQL Server topic removed.
0
 

Author Comment

by:bmanmike39
ID: 41860209
I'm trying to delete the row from my records, not check if it  exist.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 14

Expert Comment

by:Lokesh B R
ID: 41860223
Yes, debug the code see whether it works or not.
0
 

Author Comment

by:bmanmike39
ID: 41860302
I'm  a little confused. Doesn't this code do the same thing.

NOTE: It's very Important that It only deletes the sing record.

MYENTITIES oe = new MYENTITIES();
            var f = oe.fooItems.Where(s => s.fooItemId = Id).FirstOrDefault();
            oe.fooItems.Remove(f);
            oe.SaveChanges();

Open in new window

0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 41860306
Hi bmanmike39;

This should work. If that does not work for you please post the exception you are getting and inner exception.
MYENTITIES oe = new MYENTITIES();
fooItem j = oe.fooItems.ToList().Where(d => d.fooItemId == Id).FirstOrDefault();

if( j != null ) { 
    oe.fooItems.Remove(j);
    oe.SaveChanges();
} else {
    // Record was not found
}

Open in new window

0
 

Author Closing Comment

by:bmanmike39
ID: 41860348
Thank you!  This was just what I was looking for.
0

Featured Post

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.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 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