0

I'm trying to create an one to many relation, where a TypeOfImmobile can have one Immobile or more. Here's my model classes :

public class TypeOfImmobile
{
    public int Id { get; set; }
    [Display(Name = "Type Of Immobile")]
    public string Designation { get; set; }

    public ICollection<Immobile> Immobiles { get; set; }
}

public class Immobile
{
    public int Id { get; set; }
    public int TypeOfImmobileId { get; set; }
    public virtual TypeOfImmobile TypeOfImmobile { get; set; }
}

Error occurred when I create an Immobile:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Immobiles_dbo.TypeOfImmobiles_TypeOfImmobilesId". The conflict occurred in database "aspnet-ARQSI-IT2-20151031052056", table "dbo.TypeOfImmobiles", column 'Id'. The statement has been terminated.

5
  • What is the exact error? Commented Nov 1, 2015 at 23:40
  • public virtual ICollection<Immobile> Immobiles { get; set; } maybe you missed virtual Commented Nov 1, 2015 at 23:43
  • I tried virtual it doesn't work :/ Commented Nov 1, 2015 at 23:51
  • How exactly did you create an Immobile? Could you show us the code? Commented Nov 2, 2015 at 2:24
  • If you still need an answer you should show the code that causes the exception. Commented Mar 5, 2019 at 21:22

4 Answers 4

3

Type of your TypeOfImmobileId property is int, so you can't set it as null. Even you don't assign a value to an integer, it would be equal to 0 not null. If you didn't assign a value for TypeOfImmobileId when your create command was sent to SQL, SQL will take it as 0 in stored procedure that create the record. And probably TypeOfImmobile table doesn't contain a record with a 0 value in its primary key. In other words, value of foreign key of the data you sent is invalid. It doesn't correspond any primary key value in TypeOfImmobile table. Your primary key should not be nullable, but foreign key should be nullable.

You should change type of TypeOfImmobileId from int to Nullable<int> or simply int?.

So TypeOfImmobileId property should be like this:

public int? TypeOfImmobileId { get; set; }

You don't need to add ForeignKey attribute to TypeOfImmobileId property because you follow Code-First conventions and name of Foreign Key matches with name of Primary Key of related entity

You can observe the problem as running SQL Server Profiler. When application runs the command that will create the record, related stored procedure will be listed.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, great you're joining us in the Entity Framework tag. One little tip though. Usually it's a pretty hopeless operation to answer unclear questions. The question is unclear because it doesn't show the code that causes the exception. I don't think the FK should be nullable. We really need to see the code to tell what's wrong.
0

this causes by one of following reasons:

1-You don't set navigation property (TypeOfImmobile) or TypeOfImmobileId

2-The value of TypeOfImmobileId or TypeOfImmobile is not exist in database

3-Maybe you are saving Immobile before saving TypeOfImmobile

Comments

0

It seems that when you are creating your Immobile you are probably not setting the TypeOfImmobileId property with a valid value, that is, the Id of an existing TypeOfImmobile.

Comments

0

You need to set the FK by [ForeignKey("Your_FK_ImmobileId")] in a new property id Your_FK_ImmobileId

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.