delete rows using linq in c#

3 posts, 0 answers
  1. Richard M
    Richard M avatar
    76 posts
    Member since:
    Mar 2010

    Posted 20 Sep 2010 Link to this post

    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. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 21 Sep 2010 Link to this post

    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
  3. DevCraft banner
  4. Richard M
    Richard M avatar
    76 posts
    Member since:
    Mar 2010

    Posted 21 Sep 2010 Link to this post

    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();

Back to Top