コード例 #1
0
        public IHttpActionResult DoWork()
        {
            int tempId = 0;

            try
            {
                using (var scope = new TransactionScope())
                {
                    var a = new AModel {
                        BValue1 = Guid.NewGuid().ToString()
                    };
                    var b = new BModel {
                        BValue1 = Guid.NewGuid().ToString()
                    };

                    _contextA.ATable.Add(a);
                    _contextA.SaveChanges();

                    tempId = a.Id;
                    throw new Exception();

                    _contextB.BTable.Add(b);
                    _contextB.SaveChanges();

                    scope.Complete();
                }
            }
            catch
            {
                return(Ok($"Id A supposed to get - {tempId}"));
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            ContextA ctxA = new ContextA();

            ctxA.Set <ModelA>().Add(new ModelA());
            ctxA.SaveChanges();

            ContextB ctxB = new ContextB();

            ctxB.Set <ModelB>().Add(new ModelB());
            ctxB.SaveChanges();

            ContextJoin ctxJoin = new ContextJoin(ctxA, ctxB);

            ctxJoin.Set <ModelB>().Add(new ModelB());
            ctxJoin.Set <ModelA>().Add(new ModelA());
            ctxJoin.SaveChanges();

            var crossQuery = ctxJoin.Set <ModelA>().Join(
                ctxJoin.Set <ModelB>(), t => t.Id, t => t.Id, (a, b) => new
            {
                a.Name,
                b.Date
            }).ToList();

            crossQuery.ForEach(t => Console.WriteLine($"Name: {t.Name}, Date: {t.Date}"));
        }