none
Wierd error when adding new records RRS feed

  • Question

  • Dear all,

    I have the follwing model picture :

    The only thing I am trying to do is to add a new Lead record in database but get a bit confuse on the way to do

    I have done the following :

    using (ProductsEntities context = new ProductsEntities())
                {
                    Model.Lead l=new Model.Lead();
                    l.Adress = lead.Adress;
                    l.City = lead.City;
                    l.Comapany = lead.Company;
                    l.Email = lead.Email;
                    l.FirstName = lead.FirstName;
                    l.LastName = lead.LastName;
                    l.LeadJobId = lead.JobId;
                    l.Phone = lead.Phone;
                    l.Salutation = lead.Salutation;
                    l.Role = lead.Role;
                    l.ZipCode = lead.ZipCode;
                    context.AddToLeads(l);
                    context.SaveChanges();
                }

    Lead parameter is a Model type that is passe to my method, and l variable is the lead entity I need to create to be added.
    When doing so, at the time the SaveChanges is executed I get the follwoing error on a foreign key conflict but I am confuse why and how to solve :

    "The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_Lead_LeadJob\". The conflict occurred in database \"Quizz\", table \"dbo.LeadJob\", column 'Id'.\r\nThe statement has been terminated."}"

    Any help would be appreciate

    regards

    serge


    Your knowledge is enhanced by that of others.

    Wednesday, July 18, 2012 9:32 PM

Answers

  • Hello Fernando,

    It works now, I manage to find it out. The reason was that a field was not populated in my code and in my model it was set to not be null.

    But what is strange on this, why at that time it was not simply reporting as error "Field cannot be nul" instead of previous error I get .

    regards

    serge


    Your knowledge is enhanced by that of others.

    Thursday, July 19, 2012 7:37 AM

All replies

  • Hi Serge;

    Can you post the SQL statements that would create the four tables and there relationships as can be gotten from SQL Server Management please.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, July 19, 2012 2:24 AM
  • Helleo Fernando,

    It is really confuse sometimes with this entity to know really what happen. It is tru that it save some time on building the whole engine behind but after this when debugging it is an other issu from me.

    You can get the Sql statement from that link.

    Hope you can help me to solve, tackling on this too long now

    regards

    serge


    Your knowledge is enhanced by that of others.

    Thursday, July 19, 2012 6:30 AM
  • Hello Fernando,

    It works now, I manage to find it out. The reason was that a field was not populated in my code and in my model it was set to not be null.

    But what is strange on this, why at that time it was not simply reporting as error "Field cannot be nul" instead of previous error I get .

    regards

    serge


    Your knowledge is enhanced by that of others.

    Thursday, July 19, 2012 7:37 AM
  •   

    Glad to see that you got your issue corrected. And I don't have any idea why the error message is not more specific but then there are a lot of error messages that fall in that category.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, July 19, 2012 2:33 PM