将Null插入int字段 [英] Inserting Null to int field

查看:97
本文介绍了将Null插入int字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试使用Entity Framework将null插入数据库中的int字段。

该字段是外键并且可以为空。



但是在保存更改时它会限制约束。



帮助我。 ..



先谢谢....

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....

推荐答案

如果这个字段是外国的key 那么这意味着它意味着它在某些表中是主键。现在主键如何 null



FOREIGN KEY约束不必仅链接到另一个表中的PRIMARY KEY约束;它也可以定义为引用另一个表中UNIQUE约束的列。 FOREIGN KEY约束可以包含空值;但是,如果复合FOREIGN KEY约束的任何列包含空值,则会跳过对构成FOREIGN KEY约束的所有值的验证。要确保验证复合FOREIGN KEY约束的所有值,请在所有参与列上指定NOT NULL



检查这个 [ ^ ]
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[^]


您可以使用:



You Can use this :

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

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



Omarsoft For IT Solutions


你可以使用

int吗? i = null

这将帮助你
you can use
int? i = null
this will help you


这篇关于将Null插入int字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆