I am new to C# and Razor, and I having some trouble with Id as primary key (using EF). I am setting (Is Identity) as off and I want to record some random number for the field, but it keeps giving me an error:
An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code
Additional information: An error occurred while updating the entries. See the inner exception for details.
When I set id as nvarchar it is ok, or if I set another field as primary key for example Name (nvarchar) then id (int) works, but once again I need to keep it as primary key. So if someone can help. Thanks guys.
Because updating a primary key in your table is never a good idea, you want this field to be excluded from you MVC app and have it autogenerated instead.
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 am developing a web app on an existing database which unfortunately I cannot change.
I get this error
"More than one property map found for property 'Id' when using case-insensitive search. "
After a little search I found that the problem is that there are some conventions in EF and more specifically one that states (according to what I read here) that:
if the field name contains a suffix of ID (case-insensitive), then EF will automatically include it as a primary key
The table has already a primary key (named 'id').
Is there a way to disable the EF conventions or in any other way to allow having besides my 'id' field, a field named "something_id" without getting this error?
this is how to disable the id convention of EF:
inside OnModelCreating
modelBuilder.Conventions.Remove<IdKeyDiscoveryConvention>();
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 new to MVC & EF but I am getting a "ConstraintException This property cannot be set to a null value" on a particular entity when I test the client side validation. I generated the ef model from an already existing database. The table in question (ItemTypes) has a primary key that is referenced in 2 other tables (Items and ItemTypeAttributes respectively).
If I try to create an Item after filling in no fields I get a client side validation firing off, the text box turns red and i get the correct validation message. Now if I try to create an ItemType without entering a name, I get the ConstraintException, and THEN after hitting resume in Visual Studio, I see the client side validation error message and red text box.
The message says to relax or turn off constraints in the Dataset, be sure you are not trying to assign a value to a primary key field ... and clear datasets before loading them from view state.
Please advise.
-- edit --- about 20 minutes later I decided to remove the foreign key constraint from the db and update model from database. This works but it's not what I ultimately want. I would like to have referential integrity between the tables. So I can continue testing other parts of the application but I'm sure there are other relationships in my schema that I will come across with similar issues.
I had a similar problem and after further investigation was able to solve this by ensuring the properties in my entity were set to be nullable.
See my answer for further details.
how this error can be removed
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Address_ContactPerson". The conflict occurred in database "C:\USERS\SAHIL\DOCUMENTS\DOWNLOADS\COMPRESSED\N-LAYERDESIGN\N-LAYERDESIGN\APP_DATA\NLAYER.MDF", table "dbo.ContactPerson", column 'Id'.
pls help?
This error can be avoided by either respecting the foreign key constraint or removing this constraint in the database.
The ContactPersonId in your Address table doesn't have a matching record in your ContactPerson table.
Make sure the ContactPerson is added first.
From the name (FK_Address_ContactPerson) and error it seems like your contact doesn't have an address.
Are you trying to insert a contact where the there is an invalid value in the the address id on the contact, but where the corresponding Address doesn't exist?