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.
Related
This is doing my head in. I have a pretty simple model class like this (generated by EF Core Power Tools):
In the Azure SQL database, I have the following foreign key relationship in place which associates a Sighting record with to every MachineLearningTaggedImage record:
ALTER TABLE [dbo].[MachineLearningTaggedImage] WITH CHECK
ADD CONSTRAINT [FK_MachineLearningTaggedImage_Sighting]
FOREIGN KEY([SightingId]) REFERENCES [dbo].[Sighting] ([SightingId])
GO
ALTER TABLE [dbo].[MachineLearningTaggedImage]
CHECK CONSTRAINT [FK_MachineLearningTaggedImage_Sighting]
GO
I am then trying to do a very run of the mill insert, with the SightingId value populated (because I want the newly inserted MachineLearningTaggedImage record to link to an existing Sighting record that already exists in the database):
var newImageTag = new MachineLearningTaggedImage
{
SightingId = sighting.SightingId
};
_db.MachineLearningTaggedImages.Add(newImageTag);
Save();
But the insert fails with the following error:
The MERGE statement conflicted with the FOREIGN KEY constraint "FK_MachineLearningTaggedImage_Sighting". The conflict occurred in database "MyDatabase", table "dbo.Sighting", column 'SightingId'.
I'm puzzled because my existing value of SightingId (3398670) definitely exists in the Sighting table as an existing record which I want to link to.
But for some reason, EF Core or SQL won't let me do this insert.
Now if I go directly into a SQL query window (outside of EF Core), I can successfully insert the new MachineLearningTaggedImage record with a SightingId value = 3398670 - no problem at all. It works..
What am I running into? Is it something related to me having only populated the SightingId value, but the actual Sighting child / related object property is still null when I attempt to insert? I thought you could simply populated an ID of a related object and EF Core would be happy with that.
I have Table
eventid int -- not PK key but with autoincrement
jobid -- PK autoincrement disabled
userid int -- PK autoincrement disabled
To update jobID I do following:
var itemforupdate = context.table.where(n=>n.eventid == someparameter).FirstorDefault()
I get the item from database correctly, but when assigning:
itemforupdate.jobID = 5;
context.SaveChanges();
after context.SaveChanges() I get the error:
The property 'jobID' is part of the object's key information and
cannot be modified
How to update jobID from Entity Framework to solve this problem?
Updating primary key columns is not a good practice with EntityFramework. It confuses EF because it changes the identity of the object, and makes keeping the in-memory copy and the in-database copy of the data in sync very problematic. So it's not allowed.
Just don't update primary keys. Instead delete one row and insert a new one.
Alternatively you can update the primary key directly with a stored procedure or other query.
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
When I create a new entity in the EF using a model generated from a database that I already created in SQL Server, obviously I have an option to set the entity's ID(Primary Key) within my code whilst declaring the new entity.
For example when I create a new instance of my 'Value' table in my code like this:
Value newValue = Value.CreateValue(int valID);
I pass in a number say 12 to valID:
Value newValue = Value.CreateValue(12);
but when I check the Value table's data in SQL Server the primary key value valID for the new entity is assigned to something other than 12, for example 7. Why is this happening?
Thanks in advance.
Check if your primary key in the database is set up as AUTONUMBER (In Enterprise Manager go to Design mode for a table and check column properties). It means that the database will disregard whatever value you are passing and generate primary key itself.
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.