Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I have 2 table Enterprise and Enterprise Detail, the relationship is 1---*.

I created 2 dto class dtoEnterprise and dtoEnterpriseDetail, in dtoEnterprise has list dtoEnterpriseDetail.

When I insert an Enterprise, I pass a dtoEnterprise and parse to Enterprise entity(ent), and I loop list dtoEnterpriseDetail to parse to EnterpriseDetail entity(tmp), and add to ent, like ent.EnterpriseDetails.Add(tmp).

when I'm debug, the cursor run to SaveChanges methods and throw an error: "An error occurred while updating the entries. See the InnerException for details.", when I remove the parse list enterprise detail, it run normal.

How I overcome that?

Below are my code to insert to Enterprise and EnterpriseDetail tables

C#
public static bool createEnterprise(dtoEnterprise enterpriseToCreate)
        {
            try
            {
                Enterprise tmp = new Enterprise();
                tmp.EnterpriseID = enterpriseToCreate.Id;
                tmp.TaxCode = enterpriseToCreate.TaxCode;
                tmp.UserName = enterpriseToCreate.Username;
                //...
                //parse to Enterprise
                //...

                //loop
                if (enterpriseToCreate.ListEnterpriseDetail != null && enterpriseToCreate.ListEnterpriseDetail.Count > 0)
                {
                    EnterpriseDetail tmp2;
                    for (int i = 0; i < enterpriseToCreate.ListEnterpriseDetail.Count; i++)
                    {
                        //parse to EnterpriseDetail and add to tmp
                        tmp2 = new EnterpriseDetail();                       
                        tmp2.BizName = enterpriseToCreate.ListEnterpriseDetail[i].BizName;
                        tmp2.AboutUs = enterpriseToCreate.ListEnterpriseDetail[i].AboutUs;
                        //...
                        tmp.EnterpriseDetails.Add(tmp2);                        
                    }
                }
                
                _entity.AddToEnterprises(tmp);
                _entity.SaveChanges();  // throw an error here
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
Posted
Comments
Soulus83 20-Feb-12 13:05pm    
Do you know what the InnerException is? Please past the message to see if its something we can manage.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900