EF4 and Server Generated UniqueIdentifier Id - entity-framework

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.

Related

TableAttribute in EF 4.4, where is the old constructor with the TableName?

My project needs ef and we need to update to EF4.4 because of using .NET 4.0. So in this case, i changed the references to the new assembly. I get an build error that my TableAttribute(TableName) does not exists anymore. There is another Table attribute in DataAnnotations.Schema which does not take the table name. Im localizing automatically by using that Table attribute, so where is it gone in EF Version 4.4. Is there an alternative where i can add an aspect to the entity which specifies the table which is assigned later?

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

How to get last created ID from SQL database using Linq-to-SQL / Linq-to-Entities (MVC)

I want to get the last entry into the database, I usually used SCOPE_IDENTITY() in ado.net to get the last created id, but how to achieve that in Linq-to-SQL or Linq-to-Entities?
For instance I need to get the last added bookingID from the booking table whose userID=xxxx.
Both linq-to-sql and EF do this automatically. You just need to insert an object, save/submit changes and check id property in your object. If correctly configured it should be filled with scope identity.
In case of EF the property must be configured with StoreGeneratedPattern.Identity
In case of Linq-to-Sql the property must be configured with IsDbGenerated = true (Auto Generated Value in dbml designer)

How do I get Entity Framework to only update the Properties modified in the SQL generated?

I am using Entity Framework with the Self-Tracking Entity T4 templates, which by default will generate a SQL Query setting all the properties on the Entity in an UPDATE statement. I only want an UPDATE statement that contains the properties modified.
I modified the T4 Template as specified in the book: Entity Framework Recipes: A Problem-Solution Approach page 503.
I changed to this line in the T4 Template:
OriginalValueMembers originalValueMembers = new OriginalValueMembers(false, metadataWorkspace, ef);
Making the Entity track each property change instead of just tracking that the Entity changed.
And also
context.ObjectStateManager.ChangeObjectState(entity, EntityState.Unchanged);
After making these changes I got the desired result of SQL statement with only the modified values/properties in the UPDATE statement. However, there was a strange side effect. When updating a nullable INT property from null to something other than null, the change of that was ignored by Entity Framework. The Self-Tracking Models show the change in the ChangeTracker with the accurate OriginalValue null, but when Entity Framework tried to generate the UPDATE SQL it did not see that property change if the original value null and the new value is not null. I worked if the original value was not null and value gets changed.
It seems to work ok on a string property going from null to a non-null value, but int? is not working.
Does anyone have any ideas?
If helpful, have found post that fixes this: fix update of nullable column.

Entity Framework - Mapping decimal(13,0) problem

I'm migrating the aplication of my company (that nowadays run over SQL Server and Oracle) to ASP NET MVC and Entity Framework for persistence.
A create my Entity Model based on SQL Server Database e separately I create a SSDL for Oracle (for Oracle I use DevArt dotConnect for Oracle Provider) and I get some pain troubles.
My table primary keys are on SQL Server are of type decimal(13,0) and on Oracle are number(13,0) but Oracle map it's type to Int64 and SQL Server to decimal, but I need that SQL Server map it to Int64.
I make these modification manually on Entity Data Model and for create records it's works fine, but when I have to delete or update some record I got these error:
The specified value is not an instance of type 'Edm.Decimal'
Parameter name: value
at System.Data.Common.CommandTrees.DbConstantExpression..ctor(DbCommandTree commandTree, Object value, TypeUsage constantType)
at System.Data.Mapping.Update.Internal.UpdateCompiler.GenerateValueExpression(DbCommandTree commandTree, EdmProperty property, PropagatorResult value)
at System.Data.Mapping.Update.Internal.UpdateCompiler.GenerateEqualityExpression(DbModificationCommandTree commandTree, EdmProperty property, PropagatorResult value)
at System.Data.Mapping.Update.Internal.UpdateCompiler.BuildPredicate(DbModificationCommandTree commandTree, PropagatorResult referenceRow, PropagatorResult current, TableChangeProcessor processor, Boolean& rowMustBeTouched)
at System.Data.Mapping.Update.Internal.UpdateCompiler.BuildDeleteCommand(PropagatorResult oldRow, TableChangeProcessor processor)
at System.Data.Mapping.Update.Internal.TableChangeProcessor.CompileCommands(ChangeNode changeNode, UpdateCompiler compiler)
Someone can help me?
Why Entity Framework mapping are so fixed? It could be more flexible?
Ps.: The error that I got, I suspect that is because of a association.
I have a Entity named Province and another named Country and I think that the association between these Entities are causing the problem at update and delete.
Regards,
Douglas Aguiar
This may or may not help you, but i had the same error from doing this same thing. So I edited the Conceptual model and change the primary key field from Int32 to Decimal. So far, seems to have fixed things. I still need to test again against Sql Server and make sure this didnt break it.
I was getting the error "The specified value is not an instance of type 'Edm.Decimal' Parameter name: value" as you posted in your question. I had changed the default data types from Decimal to Int32 as this better reflects the true typing. When I first hit this error I rolled back the type changes and was still getting an exception but it changed just slightly but led to further digging. Bottom line, in my scenario we were expecting a trigger to populate the PK during persistence via Before Insert directive. The problem was that the domain class built by EF was setting the PK at 0 so the trigger was never firing as the incoming PK was not null. Of course EF will not let you set the Entity PK to be nullable. Maybe this will help someone else in the future.