EF add data when the insert failed id can not be null, but we have the value assigned.
System.Data.SqlClient.SqlException: cannot insert value NULL into column ‘ID’, table ‘dbo.Sys_MenuBasis’; column is not allowed to have NULL value.
This is caused by ef treating data with id attribute as self-adding id
Solution:
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);
}
}
Or add to the entity primary key attribute
public class Sys_MenuBasis
{
[System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None)]
public long ID { get; set; }
}
Done!
Similar Posts:
- mysql: [Err] 1075 – Incorrect table definition; there can be only one auto column and it must be d…
- System.Data.SqlTypes.SqlNullValueException: ‘Data is Null. This method or property cannot be called on Null values.’
- [Solved] org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
- Program error: the table or view does not exist [Solved]
- [Solved] MySQL ERROR 1062 (23000): Duplicate entry ‘0’ for key ‘PRIMARY’
- [Solved] Sping controller Receives list entity parameter Error: nested exception is java.lang.IndexOutOfBoundsException: Index: 256, Size: 256
- [Solved] MySQL uses the workbench tool, and the table status is read only
- org.hibernate.PersistentObjectException: detached entity passed to persist encountered by jpa
- Android Exception: UncaughtException detected: java.lang.RuntimeException: Parcelable encountered IOExcepti
- MySQL database insert into statement with parameters Error