none
Wierd conflicted with the FOREIGN KEY using EF RRS feed

  • Question

  • Dear all

    I have a weird issue while entering a data into one table which reference as a FK in an other

    If I enter manually the following in my PropertyType Table :

    INSERT INTO [RealEstateAgent].[dbo].[PropertyType]
               ([PropertyId]
               ,[TypeName]
               ,[PropertyTypeId])
         VALUES
               (123,'test',0)
    GO

    Then I get the following error :

    The INSERT statement conflicted with the FOREIGN KEY constraint "FK_PropertyType_Vebra_PropertyDetails". The conflict occurred in database "RealEstateAgent", table "dbo.Vebra_PropertyDetails", column 'Id'.

    Which is correct because the PropertyId value I am inserting is not existing in the PropertyDetails. So if I enter instead the following querry :

    INSERT INTO [RealEstateAgent].[dbo].[PropertyType]
               ([PropertyId]
               ,[TypeName]
               ,[PropertyTypeId])
         VALUES
               (24072341,'test',0)
    GO

    Then all works fine and data gets inserted correctly. So far so good.

    The problem is occurring when I am inserting the records above using Entity Framework as follow :

    RealEstate_Model.PropertyType A = new RealEstate_Model.PropertyType();
                        A.PropertyId = Tp.PropertyId;
                        A.TypeName = Tp.Name;
                        A.PropertyTypeId = (int)(from t in context.GetPropertyType(int.Parse(Tp.SearchCriteria)) select t).FirstOrDefault();
                        context.PropertyTypes.AddObject(A);
                        context.SaveChanges();

    All field valule are the same as in the succesfull query I was executing manually but when the context.SaveChanges() is executed, I get and exception with same error I get with the first querry I was executing manually.

    Did I miss something here ?

    regards

    Wednesday, January 29, 2014 2:53 PM

Answers

All replies

  • Hello,

    >>The INSERT statement conflicted with the FOREIGN KEY constraint "FK_PropertyType_Vebra_PropertyDetails". The conflict occurred in database "RealEstateAgent", table "dbo.Vebra_PropertyDetails", column 'Id'.

    This error should be clear that there is not a correspondent record in the master table.

    >>(int)(from t in context.GetPropertyType(int.Parse(Tp.SearchCriteria)) select t).FirstOrDefault();

    What does the GetPropertyType() method return?

    Have a try use the direct value like below:

    RealEstate_Model.PropertyType A = new RealEstate_Model.PropertyType();
    
                        A.PropertyId = 24072341;
    
                        A.TypeName = “test”;
    
                        A.PropertyTypeId = 0;
    
                        context.PropertyTypes.AddObject(A);
    
                        context.SaveChanges();
    

    To check whether it will throw the error again.

    If it throws the error again, could you please share related table schema and make a simple demo which can reproduce it and upload it to skydriverso that we can download it and test it to check why it throws such an error.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 30, 2014 3:14 AM
    Moderator
  • solved, grrrr simple refresh of model cause it to work
    • Marked as answer by wakefun Friday, January 31, 2014 8:07 AM
    Friday, January 31, 2014 8:07 AM