1

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;
    }

4 Answers 4

5

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

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
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.
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, 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.
1

try

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

3 Comments

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?
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?
I am not sure that understand your question but will try to answer, it is auto generated classes, LINQ2SQL generates that classes for you
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.