none
the classes in my dbml file don't have Add(), Update, Delete() methods RRS feed

  • Question

  • hi All

    i'm a new started in Linq to Sql and i have a problem . I've searched in other site but couldn't find similar problem and it's solution. I'm confused.

    I have a simple database with 3 tables (User ,Role , userRole). user and role have a one to many relation with userRole table. I add a linq to sql file(it's name is dblinqObjects) to my Windows project(2010) , and then drag my 3 tables in the design surface and then build the solutin , now i have a dbml file in my project that have 3 classes (roles,users,userRoles). but when i want to insert a new instance of them like a new user , there isn't Add() method .

    my code is here :

     

     DBLinqObjectsDataContext db = new DBLinqObjectsDataContext("Data Source=JOODI-PC;Initial Catalog=master;Integrated Security=True");

    user newuser = new user();

     newuser.usreID =Guid.NewGuid();

    newuser.name = "farnaz";

    newuser.family ="joodi";

     now i want to write :  db.user.Add(newuser);  but the problem is hare that  db.user doesn't have Add() method( and also Delete() and Update()).

    Thanks for any help in advance.

    Joodi

     

    Saturday, February 19, 2011 11:37 PM

Answers

  • Hi Joodi;

    There are no add methods to persist objects to the databas. We use InsertOnSubmit and DeleteOnSubmit and to update a record just modify the fields and when you SubmitChanges those update will be persisted to the database. See code snippet.

     

    DBLinqObjectsDataContext db = new DBLinqObjectsDataContext("Data Source=JOODI-PC;Initial Catalog=master;Integrated Security=True");
    
    user newuser = new user();
    newuser.usreID = Guid.NewGuid();
    newuser.name = "farnaz";
    newuser.family ="joodi";
    
    // Add the new user to the users collection.
    db.users.InsertOnSubmit(newuser);
    
    // Once you have inserted all the users you have to insert
    // update the database
    
    // Submit the change to the database.
    db.SubmitChanges();
    
    
    Fernando

     

    Sunday, February 20, 2011 3:48 PM
  • Hi Joodi;

    It is a shame that people who post on the internet do not update there post when changes like these happen. Maybe a "What version this applies to" should be placed at the top of the post, but any way ScottGu’s post of which I read many of wrote this to a Linq to SQL prior to .Net 3.5 which in fact had a Add and Delete methods. In one of the readers post on ScottGu’s blog,, LINQ to SQL Add and Delete methods renamed in .NET 3.5 RTM | Steve Glendinning , pointed readers to Linq to SQL Beta2 to RTM breaking changes which is a Microsoft Word document outlining all the changes made.

    Fernando

    Sunday, February 20, 2011 6:02 PM

All replies

  • Hi Joodi;

    There are no add methods to persist objects to the databas. We use InsertOnSubmit and DeleteOnSubmit and to update a record just modify the fields and when you SubmitChanges those update will be persisted to the database. See code snippet.

     

    DBLinqObjectsDataContext db = new DBLinqObjectsDataContext("Data Source=JOODI-PC;Initial Catalog=master;Integrated Security=True");
    
    user newuser = new user();
    newuser.usreID = Guid.NewGuid();
    newuser.name = "farnaz";
    newuser.family ="joodi";
    
    // Add the new user to the users collection.
    db.users.InsertOnSubmit(newuser);
    
    // Once you have inserted all the users you have to insert
    // update the database
    
    // Submit the change to the database.
    db.SubmitChanges();
    
    
    Fernando

     

    Sunday, February 20, 2011 3:48 PM
  • hi Fernandoo

    thanks for your advice . i use it and the new entity inserted successfully.

    but I've seen Add() , Delete (),.. methods in some documents like these:

    <cite>weblogs.asp.net/scottgu/.../05/.../using-linq-to-sql-part-1.aspx</cite> -
    So what is the reason of this difference ?? is there any changes between VS 2005 and VS 2010 in linq to sql ?
    is there any good and reliable document for learning Linq to Sql ?
    thanks
    joodi
    Sunday, February 20, 2011 5:10 PM
  • Hi Joodi;

    It is a shame that people who post on the internet do not update there post when changes like these happen. Maybe a "What version this applies to" should be placed at the top of the post, but any way ScottGu’s post of which I read many of wrote this to a Linq to SQL prior to .Net 3.5 which in fact had a Add and Delete methods. In one of the readers post on ScottGu’s blog,, LINQ to SQL Add and Delete methods renamed in .NET 3.5 RTM | Steve Glendinning , pointed readers to Linq to SQL Beta2 to RTM breaking changes which is a Microsoft Word document outlining all the changes made.

    Fernando

    Sunday, February 20, 2011 6:02 PM
  • Hi Fernando

    thanks for your help and usefull advices.. you really help me.

    i should update my knowledg

     

    Tuesday, March 8, 2011 8:21 AM
  • Not a problem, always glad to help.  ;=)
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, March 8, 2011 8:07 PM