找不到DeleteOnSubmit方法

时间:2011-10-24 18:35:04

标签: c# .net database linq linq-to-sql

我想从我的数据库中的表中删除行。 我有会员

private static WeightScaleEntities Weight = new Weight();

此成员包含我的数据库。在数据库中我有表:User_Activity。

我想通过给定i_UserActivityId从用户活动中删除行,如下所示:

//Get the rows for delete
var deleteUserActivities = from details in Weight.User_Activity
                                   where details.Id == i_UserActivityId
                                   select details;

现在我要删除这些行,所以我试着这样做:

foreach (var item in deleteUserActivities)
{
         m_WeightScaleEntities.User_Activity.*
}

我没有得到方法DeleteOnSubmit!

为什么呢? 还有另一种选择???

3 个答案:

答案 0 :(得分:1)

User_Activity.*:这是一个错字吗?

我认为你想要的是:

foreach (var item in deleteUserActivities)
{
     Weight.DeleteObject(item);
}

然后在对象上下文中SaveChanges()

BTW,静态对象上下文不是一个好主意。您应该仔细控制对象上下文的生命周期。

答案 1 :(得分:0)

using(WeightScaleEntities db=new WeightScaleEntities())
        {
            var deleteUserActivities = from details in db.User_Activity
                                       where details.Id == i_UserActivityId
                                       select details;
            if (deleteUserActivities.Count()>0)
            {
                db.deleteUserActivities.Remove(deleteUserActivities.First());
                db.SaveChanges();
            }
        }

答案 2 :(得分:0)

在Entity Framework中执行删除的方法不止一种, 您必须考虑要删除的值是什么?一排或更多。 当你需要从表中删除Row时,我们可以使用以下方式:

// first way
        using (WeightScaleEntities db = new WeightScaleEntities())
        {
            var deleteUserActivities = from details in db.User_Activity
                                       where details.Id == i_UserActivityId
                                       select details;
            if (deleteUserActivities.Count() > 0)
            {
                db.deleteUserActivities.Remove(deleteUserActivities.First());
                db.SaveChanges();
            }
        }

此行deleteUserActivities.Count()>0检查您是否有查询结果。 如果查询返回行集删除第一行,则此deleteUserActivities.First()。 "如果您不了解表格中的数据,可以使流程更加安全;"

// second way
        using (WeightScaleEntities db = new WeightScaleEntities())
        {
            var deleteUserActivities = (from details in db.User_Activity
                                        where details.Id == i_UserActivityId
                                        select details).SingleOrDefault();
            if (deleteUserActivities != null)
            {
                db.User_Activity.Remove(deleteUserActivities);
                // or use this line
                //db.Entry(deleteUserActivities).State = System.Data.Entity.EntityState.Deleted;
                db.SaveChanges();
            }
        }

您还可以使用Single或SingleOrDefault来获取单个对象。如果结果包含多个元素,Single或SingleOrDefault将抛出异常。使用Single或SingleOrDefault,您确定结果只包含一个元素。如果结果有多个元素,那么肯定会有一些问题。

此外,如果您需要删除一行或多行,请使用以下方法:

using (WeightScaleEntities db = new WeightScaleEntities())
        {
            var deleteUserActivities = (from details in db.User_Activity
                                       where details.Id == i_UserActivityId
                                       select details).ToList<User_Activity>(); //<User_Activity> her name of your DbSet
            foreach(deleteObject in deleteUserActivities)
            {
                db.Entry(deleteObject).State = System.Data.Entity.EntityState.Deleted;    
            }
            db.SaveChanges();
        }

最诚挚的问候 对不起英语。