简体   繁体   中英

insert two object in database with linq

i have two sql table (master/detail) and i have one method that save the object in sql. like this:

 public int InsertDoc(MymasterModel _inputMstr, string _IsForsale)
    {
    MasterModel _Mstr = new MasterModel();
    InsertDocs ins = new InsertDocs();
    _Mstr.Date = _inputMstr.DocDate;
    .
    .
    .
    ;


    _Mstr.DtlsModel = new List<DtlDataModel>();
            _inputMstr.MyDtlDataModel.ToList().ForEach(d =>
            {
                _Mstr.DtlsModel.Add(new DtlDataModel()
                {
                    Serial = d.Serial.ToString(),
                    Qty = d.Qty,
            ...
                });
            });

    <b>return ins.InsertDoc(_Mstr)</b>;
    }

Now i want to save two docs with different type with same detail in sql. (Input and OutPut Doc) insert this docs Linked to each other like transaction. how can i do this with linq? thanks a lot

As @BenRobinson mentioned up above, you can't do it with LINQ. LIN Q is for Q ueries. You could look at Entity Framework (or another ORM) for doing Updates, Inserts, or Deletes.

EntityFramework would look something like:

var details=new details {...}
db.details.Add(details):
db.docs.Add(new doc {... , details=details});
db.docs.Add(new doc {... , detials=details});
db.SaveChanges();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM