locked
Why inserting data into two table at once is not working? RRS feed

  • Question

  • User-1917139049 posted

    This is problematic part of the code:

                FehirdeUser User = UserManager.FindByIdAsync(Model.UserId.ToString()).Result;
                OnlinePartner NOREXECO = new OnlinePartner();
    if ((ModelState.IsValid && Model != null) && (User != null)) { using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { User.IsApproved = Model.IsApproved; NOREXECO.Id = 1; NOREXECO.UserId = new Guid(UserManager.FindByName("Frank").Id); NOREXECO.RoleName = "NOREXECO"; NOREXECO.CorporateModelVersion = 3; NOREXECO.WelcomeMessageHeaderTop = Model.WelcomeMessageHeader; try { unitOfWork.Commit(); } catch (Exception e) { unitOfWork.Rollback(); ModelState.AddModelError("", e.Message); Exception InnerException = e.InnerException; int InnerCount = 0; while (InnerException != null) { ModelState.AddModelError("", "Inner exception " + ++InnerCount + ": " + InnerException.Message); InnerException = InnerException.InnerException; } } } }

    So, 

    User.IsApproved = Model.IsApproved;

    line is working quite well, preparing data to store to AspNetUsers table at commit line.

    But, next lines with NOREXECO object filled the object, but at 

    unitOfWork.Commit();

    it didn't store into OnlinePartners table.

    Here is the OnlinePartner class:

        public class OnlinePartner
        {
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int Id { get; set; }
            /// <summary>
            /// Foreign key from AspNetUsers
            /// </summary>
            [Required]
            public Guid UserId { get; set; }
            [Required]
            [StringLength(256)]
            public string RoleName { get; set; }
            public int CorporateModelVersion { get; set; }
            [StringLength(256)]
            public string WelcomeMessageHeaderTop { get; set; }
            public string WelcomeMessageTextTop { get; set; }
    
            #region Other columns - not used right now
            [StringLength(128)]
            public string Address { get; set; }
            [StringLength(32)]
            public string PostalCode { get; set; }
            [StringLength(256)]
            public string City { get; set; }
            [StringLength(256)]
            public string Country { get; set; }
            [StringLength(128)]
            public string PhoneNumber { get; set; }
            public string ContactPersonName { get; set; }
            [StringLength(256)]
            public string ContactPersonEmail { get; set; }
            [StringLength(128)]
            public string ContactPersonPhoneNumber { get; set; }
            public string Comment { get; set; }
            public string Text3 { get; set; }
            public string Text4 { get; set; }
            public string Text5 { get; set; }
            public string Text6 { get; set; }
            public string Text7 { get; set; }
            public string Text8 { get; set; }
            public string Text9 { get; set; }
            public string Text10 { get; set; }
            #endregion
        }

    Thursday, June 15, 2017 12:19 PM

All replies

  • User753101303 posted

    Hi,

    And how unitOfWork knows about NOREXECO ? For now it seems two objects knowing nothing about each other.

    Thursday, June 15, 2017 9:37 PM
  • User1771544211 posted

    Hi  DraganSimFin,

    Based on my understanding, you can insert data into multiple data tables at once by combine the queries together like the following.

    insert into table1 (...) values (...); insert into table2 (...) values (...)

    It seems that you package these queries into a class, could you please share the code of the UnitOfWorkManager here?

    Best Regards,

    Jean

    Friday, June 16, 2017 4:28 AM
  • User-1917139049 posted

    Hi PatriceSc,

    I don't know. Probably, you have a right - they don't know for each other.
    I thought if I have object of type OnlinePartner filled with some data, commit will put that data into database in appropriate table ...
    How to make that they know for each other?

    Monday, June 19, 2017 4:50 PM
  • User-1917139049 posted

    Hi Jean Sun,

    Here is the code of only UnitOfWorkManager class I have:

    using Fehirde.Data.Context;
    using Fehirde.Domain.Interfaces;
    using Fehirde.Domain.Interfaces.UnitOfWork;
    using System.Data.Entity;
    
    namespace Fehirde.Data.UnitOfWork
    {
        public class UnitOfWorkManager : IUnitOfWorkManager
        {
            private readonly MainContext _context;
            private bool _isDisposed;
    
            public UnitOfWorkManager(IMainContext context)
            {
                Database.SetInitializer<MainContext>(null);
                _context = context as MainContext;
            }
    
            public void Dispose()
            {
                if (!_isDisposed)
                {
                    _context.Dispose();
                    _isDisposed = true;
                }
            }
    
            public IUnitOfWork NewUnitOfWork()
            {
                return new UnitOfWork(_context);
            }
        }
    }
    


    Monday, June 19, 2017 5:02 PM
  • User1771544211 posted

    Hi DraganSimFin,

    How to make that they know for each other?

    A simple way is to make one as another's parameter. So for your issue, you can create a method in UnitOfWorkManager.NewUnitOfWork which uses the OnlinePartner as parameter.

    return new UnitOfWork(_context);

    Could you please share the code of the UnitOfWork here?

    Best Regards,

    Jean

    Tuesday, June 20, 2017 3:15 AM