See what is changed before executing context.SaveChanges() - sql

In Entity Framework 5, Database-first, I am getting the error message
error in saving entities which do not make foreign key properties available
when I am trying to save a single object .
I don't know if there is a problem with the definition and mappings of a field of my object, or if I have a wrong reference in there and the Framework is somehow expecting me to provide objects for the 0...N objects my object-to-save could be referencing.
How do I see what changes are going to be written? I used a library to show the SQL which is going to be sent to the database, but it seems like the error is thrown before EF tries to execute an SQL command.

Related

What is the additional functionality provided by transaction that isnt already provided by SaveChanges?

In Entity Framework, when I make changes to multiple models and at the end call SaveChanges, either all changes get stored into the database, or no changes get stored if any of them result in an error.
So this implicitly works like a transaction. So what exactly is the purpose of creating explicit transactions?

Core Data lightweight migration error

I have been trying to get the core data lightweight migration working. Ran into an very difficult issue.
I have setup up automatic lightweight migration exactly like the documents and other SO posts. And then I create a new version, select it as current, add a new field to some entity, and then ran.
Got error:
migration failed with error Error Domain=NSCocoaErrorDomain Code=134140 "The operation couldn’t be completed. (Cocoa error 134140.)" UserInfo=0xce08c10 {reason=Can't find or automatically infer mapping model for migration,
One more error:
NSUnderlyingError = "Error Domain=NSCocoaErrorDomain Code=134190 \"The operation couldn\U2019t be completed. (Cocoa error 134190.)\" UserInfo=0xcdf2d00 {reason=Each property must have a unique renaming identifier}";
I have googled for "Each property must have a unique renaming identifier" for a while and found no result at all.
From the meaning of this error message, the properties should have unique renaming identifier. I go back to my model setting and found that I did have some renaming identifier exactly same.
My question is I didn't really change the model name at all. I just add a new field. I thought renaming identifier will only be useful when trying to renaming something. Or maybe it's required no matter what. If that's the case, then why XCode doesn't give us any warning or error about it? shouldn't it be very obvious for XCode to see?
Any suggestion is welcome.
Answering to a post from the past, but you are not the only one who encountered this Can't find or automatically infer mapping model for migration problem.
So, I had the same problem, on a big project with 12 model versions, and only lightweight migrations. Just had 2 properties renamed, and everything was ok until today.
The only fix I found was to add a mapping model (New File => Core Data => Mapping Model ) to my project between my 2 last model versions.
I hope I won't have to add one for each new model update. Seems to be a migration bug in Core Data to me.
Hope this will help other people struggling with this error.
But happened on both iOS 8 / iOS 9 devices
I don't believe migration works on the simulator. I always have to delete the app from simulator and start over after making changes to the managed object model.
Test install on a device, which is already set up with the older version, to be sure your migration is successful.

Breeze and Point In Time Entities

We are creating a system that allows users to create and modify bills for their clients. The modifications need to be maintained as part of the bill for auditing purposes. It is to some extent a point in time architecture but we aren't tracking by time just by revision. This is a ASP.NET MVC 5, WebAPI2, EntityFramework 6, SQL Server app using Breeze on the client and the server.
I'm trying to figure how to get back the Breeze and our data model to work correctly. When we modify an entity we essentially keep the old row, make a copy of it with the modifications and update some entity state fields w/ date/time/revision number and so on. We can always get the most recent version of the entity based off of an entity ID and an EditState field where "1" is the most current.
I made a small sample app to work on getting Breeze working as part of the solution and to enable some nice SPA architecture and inline editing on the client and it all works... except that since our entity framework code automatically creates a new entity that contains the modifications, the SaveChanges response contains the original entity but not the new "updated" entity. Reloading the data on the client works but it would of course be dumb to do that outside of just hacking around for demo purposes.
So I made a new ContextProvider and inherited from EFContextProvider, overrode the AfterSaveEntities method and then things got a bit more complicated. Not all the entities have this "point in time" / revision functionality but most of them do. If they do I can as I said above get the latest version of that entity using its EntityId and EditState but I'm not seeing a straight forward way to get the new entity (pretty new to EF and very new to Breeze) so I'm hoping to find some pointers here.
Would this solution lie in Breeze or our DataContext? I could just do some reflection, get the type, query the updated entity and shove that into the saveMap. It seems like that might break down at some point (not sure how or when but seems sketchy). Is our architecture bad? Should we have gone the route of creating audit/log tables to store the modified values instead of keeping the datamodel somewhat smaller by keeping all of the revisions of the entities in their original tables but with the revision information and making the queries slightly more complicated? Am I just missing something in EF?
... and to head of the obvious response, I know we should have used a document database but that wasn't an option on this project. We are stuck in relational land.
I haven't tried this but another approach would be to simply change the EntityState of the incoming entity in the BeforeSaveEntities method from Modified to Added. You will probably need to also update some version field in this 'new' entity so that it doesn't have a primary key conflict with the original.
But... having built apps like this in the past, I really recommend another approach. Store your 'historical' entities of each type in a separate table. It can be exactly the same shape as the 'current' table. When you save you first copy the 'current' entity into the 'historical' table ( again with some version numbering or date schema for the primary key) and then just update your 'current' entity normally.
This might not give you the answer you expected, but here is an idea:
When saving an object, intercept save on server, you get an instance of object you need to modify, read object from database that has the same ID, put copy of that old object to legacy table in your database and continue with saving into main table. That way only latest revision stays in main table while legacy table would contain all previous versions.
So, all you would need to do is have two tables containing same objects:
public DbSet<MyClass> OriginalMyClasses{get;set;}
public DbSet<MyClass> LegacyMyClasses{get;set;}
override SaveChanges function and intercept when entry E state is Modified, read E type, get the original and legacy tables, read object O from Original with same ID as E, save O to Legacy table, and finally return base.SaveChanges(); (let it save as it is supposed to by default).

Schema specified is not valid. Errors: 'System.Data.Spatial.DbGeography' which cannot be mapped to a primitive type

I'm doing Entity Framework (v6) db first. I get the following error when trying to access data from my datacontext.
The relationship 'Model.FK_Table1_Table2' was not loaded because the type 'Model.Table1' is not available.
The following information may be useful in resolving the previous error:
The property 'Location' on the type 'Model.Table1' has a property type of 'System.Data.Spatial.DbGeography' which cannot be mapped to a primitive type.
Any idea what is causing this error?
I don't know if this is your problem, but I had a similar error when I upgraded from EF5 to EF6. The spatial data types moved namespaces. This link has all the info:
http://msdn.microsoft.com/en-US/data/dn469466
Boiled down, you need to:
Install EF6
Update any namespace/using statements
Remove references to System.Data.Entity
(Probably) refresh your EF diagram.
Probably you need to see the solution on this url https://msdn.microsoft.com/en-US/data/dn469466
I have fixed same problem after updating some content of my code from mentioned url page there they have given solution for update to EF-5 to EF-6 after updating some of references on my context class. it is working fine.

Should I use Get or Load - nhibernate?

I am wondering which one I should use in this situation. I have a dropdown list that send a value back to the server. The server currently uses load and make the object. It then grabs a value out of and tries to convert it to an enum.
After doing some reading it seems that I should just use Get as I am need to access something out of the object other than the PK.
In general, use Get if you need access to properties other than the Id itself; this makes the intention of your code much clearer and is likely more efficient in the long run. Load is great if you need to setup FK relationships when creating or updating entities without making unnecessary round-trips to the database.
For further reading, check out Ayende's article that describes this in greater detail.
Get and Load are different if lazy loading is enabled.
If you use the method Load, NHibernate does not retrieve the entity from the database, but rather creates a proxy object and the only populated property is the ID.
If you access to an other property, NHibernate will load the entity from the DB.
So in your case the best use should be Get.