This is a migrated thread and some comments may be shown as answers.

delete rows using linq in c#

2 Answers 3883 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Richard M
Top achievements
Rank 1
Richard M asked on 20 Sep 2010, 09:10 PM
I am using the OpenAccess Domain Model method, which creates the EntityDiagrams.rlinq file in my project.  I cannot find a way to delete several rows from my sql database connection.

Here is what I am using:

using System.Linq;
using System.Data.Linq;
using Telerik.OpenAccess;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var delRecords = from c in datab.Customers where c.City == "San Francisco" select c;
        datab.Delete(delRecords);
       datab.SaveChanges();
    }
}

This gets an error.  I also tried using System.Data.Linq and DeleteOnSubmit, but I do not think that the DeleteOnSubmit method works with EntityDiagrams.  Any help would be appreciated.  Thanks.

2 Answers, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 21 Sep 2010, 06:19 PM
Hello Richard M,

Could you be more specific regarding the error which arises during the deletion process?
It seems that there are some records from other tables that have a foreign key constraint pointing to the records that you try to delete. 
You should use cascading delete to delete automatically these child records, so there are no inconsistencies in your database. Let’s consider the following model:

public partial class Order
{
    ...
    private int orderID;
     
    public virtual int OrderID
    {
        get
        {
            return this.orderID;
        }
        set
        {
            this.orderID = value;
        }
    }
     
    private IList<OrderDetail> orderDetails = new List<OrderDetail>();
     
    public virtual IList<OrderDetail> OrderDetails
    {
        get
        {
            return this.orderDetails;
        }
    }
    ...
}
 
public partial class OrderDetail
{
    ...
    private int orderID;
     
    public virtual int OrderID
    {
        ...
    }
     
    private Order order;
     
    public virtual Order Order
    {
        ...
    }
    ...
}

You should set the OrderDetails collection from the Order class as dependent under the Relationship Mapping editor(Relationship Mapping section) by enabling the Dependent setting and save the diagram.
Now when you try to delete an order and it has child order details records, these child records will be deleted automatically.
Hope that helps. If any other questions arise please contact us back.

All the best,
Damyan Bogoev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Richard M
Top achievements
Rank 1
answered on 21 Sep 2010, 06:28 PM

The foreign relationship was the problem.  My code works now.  Thanks!

(using Enable Project method)

using (IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope())

              {

              scope.Transaction.Begin();

              var results =        from p in scope.Extent<Product>()

                            where p.Name == "Bob"

                            select p;

              scope.Remove(results);

              scope.Transaction.Commit();

}



(using Add Domain Model method)

TestDB_2EntityDiagrams datab = new TestDB_2EntityDiagrams();

 

        var result = from p in datab.Products

                     where p.Name == "Bob"

                     select p;

 

        datab.Delete(result);

        datab.SaveChanges();

Tags
LINQ (LINQ specific questions)
Asked by
Richard M
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
Richard M
Top achievements
Rank 1
Share this question
or