ASP.NET MVC 4 ModelState not valid when passing key - entity-framework

I have an ASP.NET MVC 4 WebApi. I use Entity Framework as my ORM (database first). The DB has only one test table with a few fields with an int "id" set as identity (auto increment).
I just try to implement basic CRUD with a javascript client, but I always get ModelState.IsValid = false when I get to the POST method. I parsed the error and saw that is was coming from the "id" field, which has a value of 0. How does the api manage this kind of auto-increment keys ? What does it expect to receive in the "id" field ?
Everything is based on the template. I didn't change any code to the WebApi generated controller. All other operations (Read, Update, Delete) are working fine.
EDIT : Note that if I bypass the ModelState.IsValid() test, the data is saved in the DB and everything works... But obviously, that's not what I want to do.
EDIT 2 : I discovered that the identity property in a databse is handled by the StoreGeneratedPattern property on the id field in the EDMX. Possible values are : "Computed", "Identity" and "None". In my case, it's set to "Identity".

Check validation not put on ID in your model.Put Id as autogenrated.

Related

Security of Hidden ID in Views

for editing a data in views we should specify an "Id" to submit to out controller. but imagine that user manually change "Id" value. for example Id=10 change to Id=15 (by some tools in browser like Inspect). in this case EF update data with Id value 15 and trick our application. what is the best solution to overcome this issue?
My Solution:
in each domain class create a property named "UniqueId" as string type and when we add data to database, this property must be valued with GUID besides "Id" value. now when we send view to user UI we must send both property "Id" & "UniqueId" as hidden, then when client post data to controller for update, our logic section shall check "Id" & "UniqueId" both together and if any nonconformity existed, we can throw exception.
please help for this issue and mentioned solution. thanks

Why is the master data service from SQL Server 2016 creating a text field as the primary ID?

If I create a new entity from excel, or from the UI and select "Generate code automatically" for the Code attribute, MDS creates an attribute of the type "Free-form - Data Type : Text, Length : 250" !? Why not "Number, Decimals: 0"?
MDS does not give you control over the attribute types for the two default fields 'Name' and 'Code' on any entity. That is unfortunately how it was designed.
In the back-end, MDS actually uses integers as the primary keys for the entities it generates. You can look at how this is done by creating a subscription view in the web-app, and then logging onto the database with SSMS and scripting the view you created. You will see a few more attributes created and used.

Entity Framework, ado.net Data Services, odata

I'm very new to Entity Framework, that's my disclaimer! I have a SQL 2008 database with 2 tables, tblModel and tblHairColor. tblModel contains a column named hairID which is a foreign key to the tblHairColor table's primary key of id.
I created the ado.net entity data model and now, following http://msdn.microsoft.com/en-us/library/dd728283.aspx, trying to access my data resources created.
My URL of http://localhost:51157/WcfDataService.svc/tblModels(1)/modelname/$value works great by returning the model's name (of record 1) from the tblModels table. However, when I try to access the hair color via http://localhost:51157/WcfDataService.svc/tblModels(1)/modelname/tblHairColor it does not work, (http 404 not found).
My entity model, generated from my SQL database, created a tblModels navigation property in tblHairColor and a tblHairColor navigation property in tblModel. It also auto generated an association of tblHairColor to tblModel (1 to *). I expected 1 to 1.
My question is what needs to be added/changed to allow this query, http://localhost:51157/WcfDataService.svc/tblModels(1)/modelname/tblHairColor, to return the models hair color?
Thanks in advance for your time.
Bob
modelname should not be used in URL, just navigational property:
http://localhost:51157/WcfDataService.svc/tblModels(1)/tblHairColor
If you want both model and haircolor, you should use $expand:
http://localhost:51157/WcfDataService.svc/tblModels(1)?$expand=tblHairColor

EF4 and Server Generated UniqueIdentifier Id

Does Entity Framework 4 not yet support having the guid Id generated at the server upon insert? I have my Id fields set to have a default value of newid(), but Entity Framework is setting the value to the empty guid. I end up with an empty guid in the database and an empty guid in the id field of my object. Is this not yet supported or do I need to changes something on my model?
Found the solution. Basically the entity framework markup isn't generated properly.
http://leedumond.com/blog/using-a-guid-as-an-entitykey-in-entity-framework-4/
(broken link, redirects to publicity)
There's a problem with the Entity Designer in Visual Studio, which means that updating the "StoreGeneratedPattern" field doesn't correctly update the generated code. In turn, this means EF passes the entity's GUID to the SQL Server, so the server doesn't run newid() or newsequentialid().
As David says, Microsoft have released a patch.

MVC2 TryUpdateModel's include attributes not ignoring validations on fields unincluded

I have a form in my MVC 2 application that allows a user to update a set of fields from a model object, this is purely an update as the model object is already existing with its required fields entered. However when I try to update a small set of fields and call TryUpdateModel on my model object it fails and my modelstate has errors based on required fields that have already been filled out.
Here is my controller code:
[HttpPost]
public ActionResult Work(int id, FormCollection forms)
{
var lead = claimRepo.GetLeadByID(id);
if (lead == null)
return View("NotFound");
if (TryUpdateModel(lead, "Lead")) {...}
}
I've even tried explicitly stating which fields to update like so
TryUpdateModel(lead, "Lead", new string[] { "Date", "UserID", ...}) {...}
And it still fails, is there some reason this doesn't ignore validation on fields not included or am I doing something wrong?
Thanks!
EDIT
I found the issue, I had a property on my class that wasn't database backed and was marked as required in metadata, so adding a getter and setter to return what the property represented caused the TryUpdateModel to pass, but I am still curious as to why the explicit include of properties didn't ignore the field I hadn't included.
Another Edit
I have a user model as well with all database backed required fields and trying to explicitly state which fields are being updated still results in modelstate errors on the fields missing in the form, but are filled in on the model object from the db that is being updated.
I am not sure when the default behaviour was changed for validating only incoming data, from what I know I think only asp.net mvc1 supported it, then it changed in asp.net mvc2 anyway you could say [Validate(false)] and allow the partial data to go through and manually do some validation. You could use a ViewModel and validate all fields on the view model if needed as a second option. A helpful link: partial validation