2013-02-25 75 views
1

每当我尝试在mySchema中创建一个新的myObj时,它会一直告诉我ID为null,但是当我运行调试器,调试器告诉我,我将没有NULL值的对象时,它的工作原理我同事的机器上,但不能在我的...当column非空时,不能将NULL插入(“schema”。“table”。“column”)

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 

这是我得到的错误:

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

--------------------------- 
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 
--------------------------- 
+0

如果在将EntityState设置为添加后设置了ID,会发生什么情况? – IronMan84 2013-02-25 16:15:34

+0

@ IronMan84它应该有所作为吗?它曾经没有它的工作,现在它随机不起作用。刚刚尝试过,没有工作,仍然是同样的错误。 – 2013-02-25 16:23:25

+0

ID列是自动递增字段吗? – 2013-02-25 17:25:02

回答

2

当我最终查看EDMX文件时,我注意到StoreGeneratedPattern被设置为Identity,当通过Entity-Framework将该ID保存到数据库时,该ID无法通过从子类继承来传递ID。

+0

为什么孩子会向父母传递价值? – jfin3204 2013-02-25 21:03:56

+0

在TPT中使用继承表。 – 2013-02-25 21:04:41

+0

这是一个特定的**类**,而不是孩子**对象**。 - 子对象是聚合的对象,而不是更具体类型的对象。 – 2013-02-25 21:08:24

0

尽管我不了解oracle,但我在某种程度上有实体框架的经验,

我假设,MyObj是在数据库中的表,

然后,

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

检查的天气,这是给予同样的例外。

+0

[ObjectContext.AddObject](http://msdn.microsoft.com/zh-cn/library/system.data.objects.objectcontext.addobject.aspx)不会仅使用一个参数。 – 2013-02-25 17:19:57

+0

是的但ObjectContext。{TableName} .AddObject()只有一个段落。对不起,如果我错了,我指的是[this](http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.addobject.aspx)。这对我在使用EF的SQL Server数据库映射中起作用。 – 2013-02-25 17:24:38

+0

对不起,错过了中间部分!不,仍然不起作用。 – 2013-02-25 17:28:26

1

我有一个实体框架的问题,没有在edmx中设置storeGeneratedPattern =“Identity”。如果你用记事本打开edmx,并找到你的实体添加这个和它的关键属性,看看是否有效。

3

您需要在实体框架模型中指定数据库不会为您生成密钥,并且EF应该使用您提供的密钥!

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

看到源: http://msdn.microsoft.com/en-us/data/jj591617.aspx#1.3

希望这有助于其他人发现这个线程!

+0

在oracle中没有这样的选项。 – SKD 2014-07-01 05:16:45

+0

为我工作。但只适用于CodeFirst而不适用于DbFirst。 – StefanG 2014-09-25 12:19:56

+2

正是我需要让我的CodeFirst运行!非常感谢! – Jan 2016-11-04 22:06:41

相关问题