C# -Entity Framework删除数据(删)

之前操作步骤见《C#-创建Database First Demo》:https://blog.csdn.net/sinat_40003796/article/details/100541514

 

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Infrastructure;//使用方法二,必须添加的命名空间

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //删除
            using (testEntities db = new testEntities())
            {
                //1.创建要删除的对象
                student st = new student { name = "mark" };
                //2.附加到EF中
                db.student.Attach(st);
                //3.标记为删除
                //方法一:
                //db.student.Remove(st);
                //方法二:
                DbEntityEntry entry = db.Entry(st);
                entry.State = System.Data.Entity.EntityState.Deleted;
                //4.执行删除SQL
                db.SaveChanges();
                Console.WriteLine("删除成功!");
                Console.ReadLine();
            }
        }
    }
}

执行结果:

C# -Entity Framework删除数据(删)_第1张图片

你可能感兴趣的:(C#Entity,Framework)