简体   繁体   English

edmx不返回自动递增的值

[英]Edmx not returning auto incremented value

I am using Oracle 11g database. 我正在使用Oracle 11g数据库。 Many tables created in database and indexer applied on Primary key. 在数据库和索引器中创建的许多表都应用于主键。 After that i used Entity Framework 5.0 to connect with database. 之后,我使用Entity Framework 5.0与数据库连接。 The issue is that , when i am saving any record in table, it doesn't send Primary key which is auto incremented value. 问题是,当我在表中保存任何记录时,它不会发送自动递增值的主键。

public HttpResponseMessage PostCategory(TBLCATEGORY tblcategory)
    {
        if (ModelState.IsValid)
        {
            db.TBLCATEGORies.Add(tblcategory);
            db.SaveChanges();
            int32 ID=tblcategory.ID;
            return ID;
        }
    }

it returns ID =0; 返回ID = 0; And one more thing, while creating any column Integer in oracle, it is showing decimal in Entity Framework. 还有一件事,在oracle中创建任何列Integer时,它在Entity Framework中显示十进制。

I am assuming that you have defined a sequence and a trigger in the Oracle database that does the auto increment of the ID column, so only the object in the EF model is not updated, but the actual entry in the database has the correct incremented ID value, right? 我假设您已经在Oracle数据库中定义了一个序列和一个执行ID列自动递增的触发器,因此仅EF模型中的对象未更新,但是数据库中的实际条目具有正确的递增ID值吧?

Then the problem is that the edmx model does not know that the column is actually an ID column where the value is generated in the database. 那么问题是edmx模型不知道该列实际上是在数据库中生成值的ID列。 You have to manually edit the edmx model. 您必须手动编辑edmx模型。 The respective column entry in the SSDL section must have the StoreGeneratedPattern property set to "Identity". SSDL部分中的相应列条目必须将StoreGeneratedPattern属性设置为“ Identity”。 This tells the model to check again the database after inserting to look up the generated ID value. 这告诉模型在插入以查找生成的ID值之后再次检查数据库。 However, everytime you update the model from the database, your manual changes are lost. 但是,每次从数据库更新模型时,您的手动更改都会丢失。

I have written a short blog post about it: http://blog.aitgmbh.de/2014/06/02/patch-for-entity-framework-models-based-on-oracle-databases/ 我撰写了一篇简短的博客文章: http : //blog.aitgmbh.de/2014/06/02/patch-for-entity-framework-models-based-on-oracle-databases/

And I have created a NuGet package that does everything for you whenever you build the project: http://bit.ly/1hbxIsO 而且我创建了一个NuGet软件包,可以在您构建项目时为您做所有事情: http : //bit.ly/1hbxIsO

This way, even after updating your edmx model, the Identity property is added again to the specified ID columns. 这样,即使在更新了edmx模型之后,Identity属性也会再次添加到指定的ID列中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM