Getting "cannot insert NULL into (" schema "." Table "." Column ") when the column is not zero

Whenever I try to create a new myObj in mySchema, it keeps telling me that the identifier is null, but when I run the debugger, the debugger tells me that the object I'm adding is not NULL. He works for my colleague, but not for my ...

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 --------------------------- 
+1
source share
4 answers

When I finally looked at the EDMX file, I noticed that StoreGeneratedPattern was set to Identity, which prevented the transfer of the identifier from the child class when it was stored in the database through the Entity-Framework.

+2
source

You need to indicate in your Entity Framework model that the database does not generate a key for you, and EF should use your provided key!

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

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

Hope this helps others who find this topic!

+3
source

I am having a problem with an entity infrastructure that does not set storeGeneratedPattern = "Identity" in edmx. If you open edmx with notepad and find your entity, add this and its key property and see if it works.

+1
source

Although I don’t know about the oracle, I have experience with Entity infrastructure,

I assume MyObj is a table in a database,

Then

 container.MyObj.AddObject(myObj); container.SaveChanges(); 

Check the weather, this gives the same exception.

0
source

All Articles