Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "cannot insert NULL into ("schema"."table"."column") when column is not null

Whenever I try to create a new myObj in mySchema, it keeps telling me that ID is null, but when I run the debugger, the debugger tells me the object I'm adding has no NULL values. It works on my colleague's machine, but not on mine...

MyObj myObj = new myObj() {
    ID = 1234,
}
container.AddObject("MyObj", myObj);
container.ObjectStateManager.ChangeObjectState(myObj, System.Data.EntityState.Added);
// container extends ObjectContext as created by the EDMX

This is the error I get:

---------------------------

---------------------------
System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> Oracle.DataAccess.Client.OracleException: ORA-01400: cannot insert NULL into ("myModel"."myObj"."ID")
ORA-06512: at line 4

   at Oracle.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, String procedure, Boolean bCheck)

   at Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, OracleConnection conn, String procedure, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, Boolean bCheck)

   at Oracle.DataAccess.Client.OracleCommand.ExecuteReader(Boolean requery, Boolean fillRequest, CommandBehavior behavior)

   at Oracle.DataAccess.Client.OracleCommand.ExecuteDbDataReader(CommandBehavior behavior)

   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)

   at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)

   at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)

   --- End of inner exception stack trace ---

   at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)

   at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)

   at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)

   at System.Data.Objects.ObjectContext.SaveChanges()
---------------------------
OK   
---------------------------
like image 868
Bob. Avatar asked Jul 22 '26 02:07

Bob.


1 Answers

You need to specify in your Entity Framework Model that the DB does not generate the Key for you and EF should use your provided key!

modelBuilder.Entity<Department>().Property(t => t.DepartmentID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

See the source: http://msdn.microsoft.com/en-us/data/jj591617.aspx#1.3

Hope this helps others that find this thread!

like image 88
pastrami01 Avatar answered Jul 23 '26 14:07

pastrami01



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!