3

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   
---------------------------
Bob.
  • 3,894
  • 4
  • 44
  • 76

4 Answers4

7

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!

pastrami01
  • 341
  • 4
  • 9
3

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.

Bob.
  • 3,894
  • 4
  • 44
  • 76
1

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.

jfin3204
  • 699
  • 6
  • 18
0

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.

diyoda_
  • 5,274
  • 8
  • 57
  • 89
  • [ObjectContext.AddObject](http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.addobject.aspx) doesn't take only one parameter. – Bob. Feb 25 '13 at 17:19
  • Yes but, ObjectContext.{TableName}.AddObject() takes only one para. sorry if I am wrong, I was referring to [this](http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.addobject.aspx). This has worked for me in SQL Server database mapping using EF. – diyoda_ Feb 25 '13 at 17:24
  • What are the columns in MyObj table. and what are their datatypes and what are the constrains. – diyoda_ Feb 25 '13 at 17:34
  • 1
    Figured it out, the EDMX `StoreGeneratedPattern` for `ID` was set to `Identity` instead of `None`. – Bob. Feb 25 '13 at 20:46