zoukankan      html  css  js  c++  java
  • EF An error occurred while updating the entries. See the inner exception for details.

    EF新增数据时报插入失败id不能为null, 但是我们有赋值。

    System.Data.SqlClient.SqlException: 不能将值 NULL 插入列 'ID',表 'dbo.Sys_MenuBasis ';列不允许有 Null 值。INSERT 失败。

    这里是因为ef把数据带有id属性都当成自增id导致的

    解决的方法

    public class ERPContext : DbContext
    {
    public ERPContext():base("ERPContext")
    {
    }
    public IDbSet<Sys_MenuBasis> Sys_MenuBasis { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    modelBuilder.Entity<Sys_MenuBasis>().Property(p => p.ID).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None);
    }
    }

    或者在实体主键属性上加


    public class Sys_MenuBasis
    {

    [System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None)]
    public long ID { get; set; }

    }

    希望能帮到有需要的你

  • 相关阅读:
    XPath 入门
    用jQuery为页面添加活力
    将xml中的数据导入到数据库
    web.config 电邮配置
    一、创建Cuisl.dll工程
    使用ASP.NET服务器控件
    VSTO install error 0x80131604
    javaScript 5
    CSS 基础
    创建第一个ASP.NET网站
  • 原文地址:https://www.cnblogs.com/xiang-wei/p/9200562.html
Copyright © 2011-2022 走看看