The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_tblStmtTask_StmtBatchID". The conflict occurred in database "CRM", table "dbo.tblStmtBatch", column 'ID'.
The statement has been terminated
I"m getting this error while saving a modification to 'tblStmtTask' .. I understand that I have a foreign key to the 'tblStmtBatch', but I haven't a clue why changing an unrelated column in the task should have any impact on the batch.
How do I decipher what the root cause of this error actually is ?
Place a breakpoint on the update statement. The FK property will be empty. In order to find the step where it is set to empty your best bet is to breakpoint the point of creation of the entity and step through it so can pinpoint the place where the FK gets set to empty.
If the FK property is empty at creation there has been a problem with mapping. If the entity is being created with the DefaultModelBinder, make sure you have provided the FK property in your form as well, e.g. using the #Html.HiddenFor HTML helper.
Related
I have created webgrid table and it is fetching data from SQL View (data is coming from database view) everything is working fine except edit and delete. Getting error:
An exception of type 'System.ArgumentException' occurred in
EntityFramework.dll but was not handled in user code
Additional information: The number of primary key values passed must
match a number of primary key values defined on the entity.
how can I fix it? I know I can not add a Primary key in View but I just tried to add Primary key on ID column but it did not let me do it.
Based on the exception message, for example, the entity being updated has 2 columns that are the primary key, but only 1 is being passed in or vice versa. It's hard to tell without seeing any code though.
The view shouldn't matter about whether a primary key exists or not; the controller should be the main component that is in play here?
I'm using a databasefirst approach with Entity framwork and MVC 4(asp.net) and do now and then get the following error:
The operation failed: The relationship could not be changed because
one or more of the foreign-key properties is non-nullable. When a
change is made to a relationship, the related foreign-key property is
set to a null value. If the foreign-key does not support null values,
a new relationship must be defined, the foreign-key property must be
assigned another non-null value, or the unrelated object must be
deleted.
Most likly I have an unintentional null-reference which comes is due to any part of the conversion step(from view, to viewmodel, to databaseobject through automapper).
What I would like to know is if there are some way to get the information WHERE the problem is, which foreign key that is causing the issue. I have tried tracing the database but it seems like the application knows before trying to save to database that there are issues. Can I somehow debug this? Get more error info?
These type of errors can be frustrating because they can be hard to track down.
I would start with purging all the data from the database (back it up if required).
Then, open the database in Management Studio and drag and drop the tables onto the visual designer. Then you get a visual clue of what the relationships look like. You might get a surprise!
These type of errors may be occurred if you changed your database schema(model) and your database have some data.
two or more tables have relationships using foreign key.
Possible Solution
drop the database and apply migrations again.
I'm using Entity Framework (DbContext with database first) with MVC. When user save from a form, I have a condition in the controller that send the entity to the update of insert method depending of some internal flag of mine.
When sending entity to the update method, I flag it to modified using context.Entry(myEntity).State = EntityState.Modified;, I call saveChanges() and everything work well.
When sending the entity to the insert method, I flag it to added using context.Entry(myEntity).State = EntityState.Added; but when calling saveChanges() I receive error about 2 fields that are required...
The problem is that thoses 2 fields are not empty and they effectively contain valid data just before saving... I have even try to force new values to thoses 2 fields just before saving but same error.
It may be usefull to mention that I'm using Devart DotConnect For PostgreSQL as db provider.
Any idea how to debug this problem?
EDIT:
Here is the error:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
When looking for this EntityValidationErrors I receive the 2 following specific errors:
The flg_actif field is required
The user_creation field is required
As mentionned before, those fields are filled with data just before saving so I don't understand what is happening.
I'm using EF v4.0.30319 (system.data.entity=> v4.0 and EntityFramework=> v4.4)
EDIT2:
Just to clarify a little bit more: The entity I'm trying to insert already exist in database. The form show the data of this database row. When saving, I decide if I update the row (this work well) but sometime, I need to insert the edited row as a new register instead of updating it to keep an history of the change in database.
Could you verify if the EntityKey property is set or null on the items you are trying to save?
If it already has a key, the context is already aware of the item, and you should use Attach instead of setting the state to added manually.
EDIT: To summarise the point from below. It looks like what you are doing is inserting a new copy of a row already associated with a context. That is almost certainly your problem. Try creating a fresh object based on your original row (i.e. copy the variable values or use a copy constructor), then add that new object.
Additionally, you should not need to set the state manually on a newly added object. You are trying to force the state here because the context doesn't see that item as a new one.
Reading up on the web I can set ConcurrencyMode=Fixed against a entity framework database field.
My understanding is that any update statements include in its where clause the original values to determine if the datacontext has changed.
(So if rows effected gets a hit then all is good otherwise we have a conflict )
Now my question is..
Do only the columns changed in the
datacontext get included in the where
clause or all columns that are
marked as fixed.
i.e.
(If I have the following setup)
name=fixedconcurrency
DateofBith=fixedconcurrency
NI=fixedconcurrency
When only the name field changes would I get:
update tbuser set name="newJason"
where Id=2 and name="oldJason" and
DateofBith="19/10/1970" and NI=1234566
or
Update tbuser set name="newJason"
where Id=2 and name="oldJason"
My goal is to only have conflicts raised when a user overwrites another users data ( At field level not record level).
A snippet from MS says that entity framework will only update the fields that a user has edited. IF all fields are included in the where clause it would make this statement redundant.
Thanks,
Jason
All of them. The intention of the feature is for something like a TIMESTAMP field, which the user cannot assign directly.
This is entity framework 4.
CurrentProperty.FMVHistories.Add(FMVPresenter.GetFMVHistoryObject());
DataLayer.AccrualTrackingEntities repository = new AccrualTrackingEntities();
repository.Properties.AddObject(CurrentProperty);
repository.SaveChanges();
Right before I call SaveChanges, CurrentProperty has 1 object in its FMVHistories collection, as it should. Right after the save, it has two - the second of which appears to be a copy of the first, both of which have their foreign keys set, properly.
All objects involved here are new. None were loaded in any way.
FMVHistory has a composite key of 3 fields, one of which is the foreign key to the property it's attached to.
Does anyone know why this second FMVHistory object is getting added?
It looks like this was related to how EF was handling dates. The designer of the FMVHistory table made it with a composite key, part of which was a date field. For some reason when EF put in the object, it truncated the seconds and such from the date it inserted, which broke a lot of weird stuff. I ended up killing the composite key and putting in an identity, and everything is great now.