Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
1.11/5 (2 votes)
See more:
Hi,

I am trying to insert null to int field in the database using Entity Framework.
That field is foreign key and nullable.

But it is giving exception of constraint while saving the changes.

Help me...

Thanks in Advance....
Posted
Comments
Toli Cuturicu 6-Oct-10 14:59pm    
Absurdity
thatraja 7-Oct-10 1:18am    
Hi, The link is working now which was not worked in Answer1.

If this field is foreign key then it means it means that it is primary key in some table. Now how can the primary key be null?

A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table. A FOREIGN KEY constraint can contain null values; however, if any column of a composite FOREIGN KEY constraint contains null values, verification of all values that make up the FOREIGN KEY constraint is skipped. To make sure that all values of a composite FOREIGN KEY constraint are verified, specify NOT NULL on all the participating columns

Check This[^]
 
Share this answer
 
v2
Comments
Member 7217874 6-Oct-10 12:36pm    
The link what you have given is not working...

But i have specified my FOREIGN Key constraint as nullable and i am doing this with my other two fields also. With them it is fine because they are TinyInt and this one is SmallInt.
You Can use this :

C#
Nullable<int> value=null;
var entity = new Model()
           {
               Dbvalue = value
           };

  DataContext.Models.Add(entity);
  DataContext.SaveChanges();</int>

Omarsoft For IT Solutions
 
Share this answer
 
you can use
int? i = null
this will help you
 
Share this answer
 

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