如何删除LINQ to SQL的? [英] How to delete in linq to sql?

查看:194
本文介绍了如何删除LINQ to SQL的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的LINQ to SQL和我不知道如何真正删除记录。

I am very new to linq to sql and I am not sure how to actually delete a record.

所以,我一直在看本教程

So I been looking at this tutorial

<一个href=\"http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx\">http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

因此​​,对于更新他们有

So for Update they have

NorthwindDataContext db = new NorthwindDataContext();

Product product = db.Products.Single(p => p.ProductName == "Toy 1");

product.UnitPrice == 99;
product.UnitsInStock = 5;

db.SubmitChanges();

有关删除它们。

NorthwindDataContext db = new NorthwindDataContext();

var toyProducts = from p in db.Producsts
              where p.ProductName.Contains("Toy")
                  select p;

db.Products.RemoveAll(toyProducts);

db.SubmitChanges();

所以,我必须对每一个时间,拿到了记录,以删除记录?就像我可以,因为你需要给它一个记录下哪些更新,然后再进行更改,所以我理解查询的一部分,但不删除排序看到与更新这样做。

So do I have to query every time, to get the record in order to delete that record? Like I can sort of see doing this with update since you need to give it a record which to update first and then make the changes so I understand the querying part but not with delete.

就像你不能只送你想删除什么,它会和删除呢?为什么你必须先得到它,然后告诉它要删除?

Like can't you just send in what you want to delete and it goes and deletes it? why do you have to first get it and then tell it to be deleted?

是不是命中2对数据库?

Is that not 2 hits on the database?

我也有,我试图去上班外键关系。所以我有这个

Also I have foreign key relationship that I am trying to get to work. So I have this

public ViewResult(string param1, string param2)
{
   Table A = new Table A
   A.Field1 = param1;
   A.Field2 = param2;

   Delete(A);
}

private void Delete(Table A)
{
   DbContext.A.DeleteAllOnsubmit(A.TableB);
   DbContext.A.DeleteAllOnSubmit(A.TableC);
   DbContext.A.DeleteOnSubmit(A);

}

所以,失败则与此消息出现无法删除尚未连接的实体。

So this fails it comes up with this message "Cannot remove an entity that has not been attached."

所以我能看到为什么第2号线将在删除方法失败了,因为我做了一个新的对象,他们什么也没有说有大约表B和表C的任何信息的对象。

So I can see why the first 2 lines would fail in the delete method, since I made a new object and their is nothing in the object that has any information about TableB and TableC.

然而,我不明白为什么最后一行仍然失败,即使2其他线路的地方不存在。

I however can't see why the last line still fails even if the 2 other lines where not there.

就像我怎么想它会工作,将采取我的表,我在传递一个类的对象,并通过表查找包含的信息。它似乎并不虽然如此。

Like how I thought it would work it would take my Table A class object that I passed in and look through the table for that information contained in it. It does not seem to be the case though.

所以,我首先必须将品牌的信息,然后做一个查询来获取它,然后将其删除,比如上例中?

So do I first have to take the information out and then do a query to get it and then delete it, like in the example?

也就是什么的removeAll(之间的差)和说DeleteAllOnSubmit()

Also what is the difference between removeAll() and say DeleteAllOnSubmit().

就像我说我是新来的LINQ to SQL和一直没能坐下来就可以读一本书,由于时间的限制。一旦我有更多的时间,我将通过一本书可能读取。

Like I said I am new to linq to sql and have not been able to sit down and read a book on it due to time constraints. Once I have more time I will read through a book probably.

感谢

推荐答案

您在你一个问题的几个问题,但我会用最简单的开始,有关如何安装,如果你已经有主键。如果没有主键,然后我一直刚刚做了提取,然后删除,但是,每当我做一个获取我倾向于存储主键的更新和删除。

You have several questions in your one question, but I will start with the simplest, about attaching, if you already have the primary key. If you don't have the primary key then I have always just done a fetch then a delete, but, whenever I do a fetch I tend to store the primary key for updates and deletes.

它会删除掉的主键的,但如果你有那么只是附加为我做以下,并呼吁删除。我没有通过围绕由DLINQ需要的,因为我希望能够改变它,如果我想的对象,所以我通过在一个不同的用户对象,并从业务类只是拉的PK,并把它放入DAO类。

It will delete off of the primary key, but if you have that then just attach as I do below and call delete. I don't pass around the object needed by DLINQ as I want to be able to change it if I want, so I pass in a different User object and just pull the PK from the business class and put it into the DAO class.

var db = new MeatRequestDataContext();            
if (input.UserID > 0)
{
     entity = new User()
     {
         UserID = input.UserID
     };
     db.Users.Attach(entity);
     db.Users.DeleteOnSubmit(entity);
 }

这篇关于如何删除LINQ to SQL的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆