Linq to entity从表中删除特定列

时间:2011-12-13 08:51:12

标签: asp.net asp.net-mvc asp.net-mvc-3 linq-to-entities

Linq to entity查询通过匹配条件来清除表中的特定列

public ActionResult deleteChecks(string checkValue)
    {
        check_master checks = (from table in db.check_master
                              where table.check_code == checkValue
                              select table).First();
        //now how to delete/remove checks.mcheck

        return View("Edit");
    }`

只想从表check_master

更新具有空值(所选行的)的单个列元素

4 个答案:

答案 0 :(得分:0)

我相信Linq to Entities只支持DML,它不支持DDL操作。 因此,您必须使用存储过程或ADO.NET原始查询。

编辑

您可以像这样进行简单的更新:

public ActionResult deleteChecks(string checkValue)
{
    check_master checks = (from table in db.check_master
                          where table.check_code == checkValue
                          select table).First();

    checks.mcheck = null;
    db.SaveChanges();

    return View("Edit");
}`

答案 1 :(得分:0)

实体框架仅适用于常量表方案。 请告诉您,您的全球目标是什么,可能有更适合的方式。

<强>更新

foreach(var chm in db.check_master)
{
    chm.mcheck = null;
}
db.SaveChanges();

答案 2 :(得分:0)

您可以将单个属性(映射到列)设置为null并将其保存到数据库

foreach(check_master check in checks)
{
   check.mcheck = null;
}

db.SaveChanges();

答案 3 :(得分:0)

using (NorthwindDataContext db = new NorthwindDataContext())

{

// Retrieve the existing entity. Database Call 1

Product product = db.Products.First(p => p.ProductID == 1);



// Change the properties. LINQ to SQL knows

// these specific properties have changed.

product.UnitsInStock = 14;



// Flush the changes. Database Call 2

db.SubmitChanges();

}