views:

346

answers:

4

Hi there,

I am trying to insert a standard record into my db using linq2db, but i keep seeing examples to ADD method which i don't appear to have ... what i have currently is the following, as you can see i have my datacontext.... (no add method) ... the Reservation class is a separate class i created as a DTO - i presume this is correct?

Any help really appreciated,

    public bool AddReservation(Reservation reservation)
    {
        bool success = false;
        try
        {
            MiningDataContext db = new MiningDataContext();


            db.Reservations. // NO ADD HERE


        }
        catch { }

        return success;
    }
+1  A: 

try

db.Reservations.InsertOnSubmit(reservation);
db.SubmitChanges();
ArsenMkrt
Thanks .. but what type is reservation .. it states "Reservation", of course i created a "RESERVATION" dto but how would linq2sql know this? I presume its talking about an entity called Reservation but i can't seem to find it, what namespace is it in?
mark smith
and more to the point, what if i don't have the DTO?... i am sure i can't pass anything here?? ... or can i pass anything that has the same "Field names" as properties?
mark smith
I am not sure that understand your question but will try to answer, it is auto generated classes, LINQ2SQL generates that classes for you
ArsenMkrt
+1  A: 

You should use the method InsertOnSubmit() and then call SubmitChanges().

public bool AddReservation(Reservation reservation)
{
    bool success = false;
    try
    {
        MiningDataContext db = new MiningDataContext();

        db.Reservations.InsertOnSubmit(reservation);
        db.SubmitChanges();

        ...

    }
    catch { }

    return success;
}
Marpe
Thanks .. yep thats there.. 1 question though... InsertOnSubmit takes a Reservation type... this can't be my Reservation DTO as i created this separately (its a dto) ... so i presume i have to do some kind of mapping??? I tried changing the namespace of Entity Classes but i can't seem to differenciate between "MY" reservation and the entity class reservation
mark smith
Well, the data context (MiningDatacontext) expects an object of the Reservation class generated by LINQ to SQL. I just assumed you have a DBML-file where you mapped up your Reservations table? The output of that is both the DataContext and the entity classes. You can choose the namespace for those classes in the properties view when you have the DBML designer open.
Marpe
Yep this is true! just confirmed i was using a T4 tool to generate my DTOs which actually removes the generated Linq2sql stuff but the generated dtos are exactly the same just in a separate file... So i need to pass the Reservation DTO and it works... Thanks for the confirmation .. much appreciated
mark smith
Mark, can I ask what T4 template you use for this? We are currently doing this manually, and would love to know more about the T4 that you are using.
Ash M
A: 

Instead of doing db.Reservations.Add, just do Reservations.Add. Then use the db.SubmitChanges()

Web
A: 

mybee you have compile errors within that class that was causing the intellisense to break i suggesting you to compile all the solution

anyway it should be InsertOnSubmit function

avnic