Edmx not returning auto incremented value - c#

I am using Oracle 11g database. Many tables created in database and indexer applied on Primary key. After that i used Entity Framework 5.0 to connect with database.
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;
And one more thing, while creating any column Integer in oracle, it is showing decimal in 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?
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. You have to manually edit the edmx model. The respective column entry in the SSDL section must have the StoreGeneratedPattern property set to "Identity". This tells the model to check again the database after inserting to look up the generated ID value. 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/
And I have created a NuGet package that does everything for you whenever you build the project: http://bit.ly/1hbxIsO
This way, even after updating your edmx model, the Identity property is added again to the specified ID columns.

Could you check the following articles:
problem description:
http://connect.microsoft.com/VisualStudio/feedback/details/505178/storegeneratedpattern-property-in-ado-net-entity-model-designer-sets-cdsl-annotation-but-not-ssdl-attribute
solution to the problem:
http://visualstudiogallery.msdn.microsoft.com/a63745c0-a781-48fa-a7d2-573ee80b5d7e

Related

EF5 doesn't track Identity oracle field after insert and saveChanges()

So, I'm trying to get the Id of the inserted object using entity framework 5 and Oracle.ManagedDataAccess but it keeps returning 0 although it inserts correctly in db.
I use an ID column with a sequence and trigger in the Database.
*note: I use database first approach.
Here is the code:
var test_trans = new TEST_TRANSACTION101() {
NAME = tran.Name,
TEST_NUMBER =tran.Test_Number
};
_context.TEST_TRANSACTION101.Add(test_trans);
_context.SaveChanges();
int id = test_trans.ID; // here it should retrieve the id from db
I used [DatabaseGenerated(DatabaseGeneratedOption.Identity)
and many other solutions I found here nothing seems to work.
It seems that ef doesn't track my entity. Any help?!
After alot of search I've found that Entity Framework misGenerate my data model. So I had to open model designer and make property Id as Identity myself.

EntityFramework is returning false for newly added bit column

I am using the entity framework for getting data from database. I recently added a new column in database and updated the EDMX model from database. In the EDMX model I can see the new field. But when I retrieve the data from database, this newly added field is always set to false. I have verified in the DB that some of the rows for this column are set to true.
False is the default value for boolean. If you're accessing the right database and you're sure about the values, you must be omitting the bit column in the select statement, so when you get the values, you see just the default value for the bit field.

Entity Framework Id auto incrementing

Have a problem with incrementing.
I created a new object and tried set it into my DB I received an error of data violation. The index in table wasn't increased (Id=0).
Id - set as primary key in SQL table and the StoredGeneratedPattern property of field "Id" in EDM set as "Identity" so, obviously, it must be incremented automatically.
public void AddPhone(UserPhone phone)
{
context.AddToUserPhone(phone);
context.SaveChanges();
}
I can't understand why.
Entity Framework does not automatically increment IDs. That's the database's job. Set the ID column on the database table as an IDENTITY column so that it will auto-increment. Then you should find that after you SaveChanges() the phone's ID property will have been set to the value the database chose for it.

Entity Framework One-To-Many Insert - Foreign Key violation

I'm using Entity Framework for the first time and I'm trying to create a object with a collection (and I want all the objects in the collection to be created in database as well) but I'm having some foreign keys violations.
My sample tables:
table APPOINTMENTS: ID, VAR1, DATE_APPOINTMENT
table GUESTS: ID, APPOINTMENT_ID, USER_ID, VAR2, VAR3
My test code:
DomainService aux = new DomainService();
APPOINTMENTS appointment = new APPOINTMENTS();
appointment.VAR1 = "BLA";
appointment.DATE_APPOINTMENT = new DateTime();
//The user with id = 1 is already created in the database
appointment.GUESTS.Add(new GUESTS { USER_ID = 1, VAR2 = 1, VAR3 = "F" });
aux.InsertAppointment(appointment);
At DomainService I have:
public void InsertAppointment(APPOINTMENTS appointment)
{
using (var context = this.ObjectContext)
{
context.AddToAPPOINTMENTS(appointment);
context.SaveChanges();
}
}
But I'm getting this error:
{"ORA-02291: integrity constraint (FK_GUESTS_APPOINTMENTS) violated - parent key not found"}
What am I doing wrong?
UPDATE:
To create the ID's in the database, I am using a sequence for each table and a trigger before insert to get the next value.
When I create a single object, e.g. a appointment without guests, it inserts in the database and it generates the id.
The solution to this problem:
"The ID fields that are generated from sequences won't be handled
correctly. After saving the entities the ID's will be returned as 0.
I'll fix this by manually hacking the SSDL (open your .edmx file in a
text editor) with StoreGeneratedPattern="Identity" attributes on the
ID fields (lines 6 and 16). Note that designer may rip that change out
upon future modification.
While I suppose it's not absolutely necessary it might also be prudent
to modify some type metadata such as changing "number"s to "int"s in
your SSDL and "Decimal"s to "Int32"s in your CSDL where applicable.
Frequently these don't auto-generate with the desired values
especially with XE."
#http://www.chrisumbel.com/article/oracle_entity_framework_ef
As for me, the problem was solved simply by opening diagram .edmx and changing property StoreGeneratedPattern from None to Identity for each Primary Key in each table. After saving everything was fine.
I'm using VS 2012, Entity Framework 5 (6 is not supported yet), Oracle 11.2, last ODP.NET 12, .Net 4.5
In case of EF code first approach, if this error come
(ORA-02291: integrity constraint (FK_GUESTS_APPOINTMENTS) violated -
parent key not found)
In my case there are 2 tables which have Identity columns. So I just added
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
property to my model class just above the the column which is identity column in database and it solved my problem :)
Hope this help :)
I cant see where you are setting your Primary Key (the ID property of the appointment class). Are you using a key generator on the database side? If not this should be the problem.
You're inserting a record with a foreign key value that is not found in the parent table that constraint refers back to.

Scope_Identity() in Entity Framework

What is the equal code in EntityFramework to this sql code?
select Scope_Identity()
I want to get the id of the last record that i have inserted to database in EF.
You must ensure that your key property is mapped with StoreGeneratedPattern.Identity (here you can find some more about this setting). This should be used as default when generating the model from MS SQL database where the key column is defined with IDENTITY. After that it is enough to call SaveChanges on the context and the key property will be automatically filled for you.

Categories

Resources