When I eventually looked at the EDMX file, I noticed that the StoreGeneratedPattern was set to Identity, which prevented the ID to be passed up with inheritance from a child class when it was being saved to the database through Entity-Framework.
Getting "cannot insert NULL into ("schema"."table"."column") when column is not null
-
11-03-2022 - |
Frage
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
---------------------------
Lösung 2
Andere Tipps
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!
I have had a problem with entity framework not setting storeGeneratedPattern="Identity" in the edmx. If you open the edmx with notepad and find your entity add this and to its key property and see if that works.
Although I do not know about oracle, I have experience in Entity framework to some extent,
I am assuming that, MyObj
is a table in the database,
Then,
container.MyObj.AddObject(myObj);
container.SaveChanges();
Check weather this is giving the same exception.