Dependency

Posts   
 
    
Posts: 54
Joined: 22-Jun-2010
# Posted on: 31-Aug-2010 05:40:16   

LLBLGEN 3.0 Oracle 9i/10g

Hi,

With steep learning curve, I have learnt lot and done most of things either on my own or by support. Now the problem is dependency.

I am trying to insert data into following tables MEMBER, MEMBERADDRESS AND MEMBERFAMILY. MEMBER_ID is oracle sequence and is set in designer and generates automatically. the same MEMBER_ID (I mean Member_ID from table member, generated auto) should be inserted into MEMBERADDRESS, MEMBERFAMILY and then commit once. the manual says I can just set the dependency injection to true in app.config and i have done so. but still am not able to insert member_id into memberfamily and memberaddress. Not sure how to get this going fine. I have attached the code just in case if i have done somthing wrong or i have not understood something right

Attachments
Filename File size Added on Approval
code.txt 2,170 31-Aug-2010 05:40.22 Approved
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 31-Aug-2010 07:38:07   

I don't know why the problem is about Dependency Injection. It looks like you need just a recursive save.

David Elizondo | LLBLGen Support Team
Posts: 54
Joined: 22-Jun-2010
# Posted on: 31-Aug-2010 08:40:11   

daelmo wrote:

I don't know why the problem is about Dependency Injection. It looks like you need just a recursive save.

Well, if you see my code, I have done recursive save but still not working

Posts: 54
Joined: 22-Jun-2010
# Posted on: 31-Aug-2010 09:07:48   

Since it not inserting i get an error Message="ORA-01400: cannot insert NULL into (\"BCLUB1868\".\"MEMBERADDRESS\".\"MEMBER_ID\")\n"

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 31-Aug-2010 09:35:26   

You are using a UnitOfWork, and adding each entity to the UnitOfWork, with save recursively flag, but you did not associate them together. (i.e. nothing relates these entities to each other).

You should do something like.

var customer = new Customer();
customer.Name = "ABC";
...
var order = new Order();
order.Date = DateTime.Now;
...
customer.Orders.Add(order); // That's the trick

// recursive save using a DataAccessAdapter
adapter.SaveEntity(customer, false, true);

//Or using a UnitOfWork2
uow.AddForSave(customer, true); // and so you don't need to add the order to the UoW.
uow.Commit(adapter);
Posts: 54
Joined: 22-Jun-2010
# Posted on: 31-Aug-2010 09:56:12   

Walaa wrote:

You are using a UnitOfWork, and adding each entity to the UnitOfWork, with save recursively flag, but you did not associate them together. (i.e. nothing relates these entities to each other).

You should do something like.

var customer = new Customer();
customer.Name = "ABC";
...
var order = new Order();
order.Date = DateTime.Now;
...
customer.Orders.Add(order); // That's the trick

// recursive save using a DataAccessAdapter
adapter.SaveEntity(customer, false, true);

//Or using a UnitOfWork2
uow.AddForSave(customer, true); // and so you don't need to add the order to the UoW.
uow.Commit(adapter);

Hi Walaa, am glad that you are here to help me out. Yes I am using UnitOfWork2. I think I have done the same way you have said. Like you mentioned "don't need to add the order to the UoW". If you can help me with the code I have attached, it would be of great help.

And you said "nothing relates these entities to each other" in UOW. so using UOW how do i relate it ? because you said no need to add the order in the example you said while using UOW

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 31-Aug-2010 10:09:42   

Your code should look like the following. - I've removed extra parts which are not relevant t our discussion. - I moved the instantiation of related entities inside the loops. - I only added the master entity to the UoW, at the end of the code.

try
{
   var memberBulkInsert = new UnitOfWork2();

   var member = new MemberEntity();
   member.Memberno = ChangeCase.ToUpperCase(tbxmemberno.Text.Trim());
   member.CategorysubgroupId = Convert.ToInt32(cmbcategorysubgroupid.SelectedValue);

   int rowCount = Convert.ToInt32(dgridmemberaddress.Rows.GetRowCount(DataGridViewElementStates.None)) ;

   if (rowCount > GlobalVariable.numberofrecords)
   {
      for (int i = 0; i < rowCount; i++)
      {
        var memberaddress = new MemberaddressEntity();
        memberaddress.Address = Convert.ToString(dgridmemberaddress.Rows[i].Cells[1].Value);
        memberaddress.Pincode = Convert.ToString(dgridmemberaddress.Rows[i].Cells[2].Value);

        member.MemberAddress.Add(memberaddress);
        int rowCount1 = Convert.ToInt32(dgridmemberfamily.Rows.GetRowCount(DataGridViewElementStates.None)) ;
      }
   }
        
   if (rowCount1 > GlobalVariable.numberofrecords)
   {
      for (int i = 0; i < rowCount1; i++)
      {
         var memberfamily = new MemberfamilyEntity();                           
         memberfamily.Userno = Convert.ToString(dgridmemberfamily.Rows[i].Cells[1].Value);
         memberfamily.Username = Convert.ToString(dgridmemberfamily.Rows[i].Cells[2].Value);
         member.MemberFamily.Add(memberfamily);
      }
   }

memberBulkInsert.AddForSave(member, true);
memberBulkInsert.Commit(adaptermember);
Posts: 54
Joined: 22-Jun-2010
# Posted on: 31-Aug-2010 10:13:34   

Walaa wrote:

Your code should look like the following. - I've removed extra parts which are not relevant t our discussion. - I moved the instantiation of related entities inside the loops. - I only added the master entity to the UoW, at the end of the code.

try
{
   var memberBulkInsert = new UnitOfWork2();

   var member = new MemberEntity();
   member.Memberno = ChangeCase.ToUpperCase(tbxmemberno.Text.Trim());
   member.CategorysubgroupId = Convert.ToInt32(cmbcategorysubgroupid.SelectedValue);

   int rowCount = Convert.ToInt32(dgridmemberaddress.Rows.GetRowCount(DataGridViewElementStates.None)) ;

   if (rowCount > GlobalVariable.numberofrecords)
   {
      for (int i = 0; i < rowCount; i++)
      {
        var memberaddress = new MemberaddressEntity();
        memberaddress.Address = Convert.ToString(dgridmemberaddress.Rows[i].Cells[1].Value);
        memberaddress.Pincode = Convert.ToString(dgridmemberaddress.Rows[i].Cells[2].Value);

        member.MemberAddress.Add(memberaddress);
        int rowCount1 = Convert.ToInt32(dgridmemberfamily.Rows.GetRowCount(DataGridViewElementStates.None)) ;
      }
   }
        
   if (rowCount1 > GlobalVariable.numberofrecords)
   {
      for (int i = 0; i < rowCount1; i++)
      {
         var memberfamily = new MemberfamilyEntity();                           
         memberfamily.Userno = Convert.ToString(dgridmemberfamily.Rows[i].Cells[1].Value);
         memberfamily.Username = Convert.ToString(dgridmemberfamily.Rows[i].Cells[2].Value);
         member.MemberFamily.Add(memberfamily);
      }
   }

memberBulkInsert.AddForSave(member, true);
memberBulkInsert.Commit(adaptermember);

I think am getting it how it works. Though I can just copy paste your code and make it work, let me understand how it is done.

As of now no issues. will revert back if need any help. Well let me say LLBLGEN has really helped me in writing efficient way