简体   繁体   English

当值不为null时,无法将NULL插入列

[英]Cannot insert NULL into column while value is not null

So im having this weird problem with trying to add an instance to the entities. 因此,尝试向实体添加实例时遇到这个奇怪的问题。 Its a asp.net application using EF 6.0. 它是一个使用EF 6.0的asp.net应用程序。

The exception: 例外:

SqlException: Cannot insert the value NULL into column 'OrderId', table 'BETA.MDF.dbo.Orders'; SqlException:无法将值NULL插入到'OrderId'列中,表'BETA.MDF.dbo.Orders'; column does not allow nulls. 列不允许空值。 INSERT fails. INSERT失败。 The statement has been terminated. 该语句已终止。

My code: 我的代码:

User user = (User)Session["CurrentUser"];
BetaEntities entities = new BetaEntities();

Beta.Order order = new Beta.Order();
order.OrderId = Guid.NewGuid().ToString();
order.OrderDate = DateTime.Now;
order.OrderedBy = user.UserId;
order.HandledBy = entities.Users.Where(x => x.Rank > 0).Select(i => i.UserId).FirstOrDefault();

entities.Orders.Add(order);

entities.SaveChanges();

My table: 我的桌子: 在此输入图像描述

Also this is my order class, i already tried [Databasegenerated] 这也是我的订单类,我已经尝试了[Databasegenerated]

public partial class Order
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public string OrderId { get; set; }
    public System.DateTime OrderDate { get; set; }
    public int OrderedBy { get; set; }
    public int HandledBy { get; set; }
}

Example entry: 示例条目:

在此输入图像描述 Please tell me if you need more details. 如果您需要更多详细信息,请告诉我。

EDIT: 编辑:

I tried setting the database datatype of OrderID to UNIQUEIDENTIEFIER and updated the entity model so the database should generate the guid(and i removed itself but i still get the same exception. 我尝试将OrderID的数据库数据类型设置为UNIQUEIDENTIEFIER并更新实体模型,以便数据库生成guid(我删除了自己,但我仍然得到相同的异常。

This is my new class: 这是我的新课程:

public partial class Order
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public System.Guid OrderId { get; set; }
    public System.DateTime OrderDate { get; set; }
    public int OrderedBy { get; set; }
    public int HandledBy { get; set; }
}

在此输入图像描述

在此输入图像描述

EDIT: 编辑:

If i make OrderID NULLABALE and remove the primary key i get the following exception: 如果我使OrderID为NULLABALE并删除主键,我会得到以下异常:

System.Data.Entity.Infrastructure.DbUpdateConcurrencyException: 'Store update, insert, or delete statement affected an unexpected number of rows (0). System.Data.Entity.Infrastructure.DbUpdateConcurrencyException:'存储更新,插入或删除语句影响了意外的行数(0)。 Entities may have been modified or deleted since entities were loaded. 自实体加载后,实体可能已被修改或删除。 See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.' 有关理解和处理乐观并发异常的信息,请参阅http://go.microsoft.com/fwlink/?LinkId=472540

But this can be resolved by adding a primary key :( so removing the PK and making it NULLABLE isnt working. 但这可以通过添加主键来解决:(因此删除PK并使其无效也无法正常工作。

Also i need to assign to GUID myself for further purposes so letting the database assign the guid itself(with [DatabaseGenerated(DatabaseGeneratedOption.Identity)] ) is also not an option. 此外,我需要自己分配给GUID用于进一步的目的,所以让数据库分配guid本身( [DatabaseGenerated(DatabaseGeneratedOption.Identity)] )也不是一个选项。

Your problem is that you set DatabaseGenerated attribute to the OrderId column. 您的问题是您将DatabaseGenerated属性设置为OrderId列。

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string OrderId { get; set; }

With this attribute EF will ignore value you set to OrderId and "try" generate unique value, but generating unique value will not work for string and OrderId will be NULL when INSERT query is generated. 使用此属性,EF将忽略您设置为OrderId值并“尝试”生成唯一值,但生成唯一值将不适用于string并且在生成INSERT查询时OrderId将为NULL

If you are using EF "code first" then change type of OrderId to Guid and remove next line from code 如果您使用EF“代码优先”,则将OrderId类型更改为Guid并从代码中删除下一行

order.OrderId = Guid.NewGuid().ToString();  

And leave DatabaseGenerated attribute as it is. 并保留DatabaseGenerated属性。
When using DatabaseGenerated attribute for column - means that value of this column will be generated by database. 对列使用DatabaseGenerated属性时 - 表示此列的值将由数据库生成。

After changing your structure of Order class, you need update database accordingly. 更改Order类的结构后,需要相应地更新数据库。 If you using "code-first" approach then you need generate migration script and run it against your database. 如果您使用“代码优先”方法,则需要生成迁移脚本并针对您的数据库运行它。 In "database-first" approach you need update database manually 在“数据库优先”方法中,您需要手动更新数据库

Even you said removing DatabaseGenerated attribute doesn't help, it will be most simpler solution as suggested by Ivan Stoev in the comments. 即使你说删除DatabaseGenerated属性没有帮助,它将是Ivan Stoev在评论中提出的最简单的解决方案。
Because you generating unique string by yourself using Guid.NewGuid().ToString() . 因为您使用Guid.NewGuid().ToString()自己生成唯一的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM