I'm implementing an application that need to keep track of the data changes, so we're going to version the entities.
One of the requirements is that when the user starts an edit, a "local" copy is created which the user will be editing. This copy actually has to be stored on the server (so not really local) so that if the user pauses the work she can retrieve it later, even from a different machine.
When happy with the changes she saves and the changes become visible to everyone. A new version number is assigned to the edited entity and to the entities referencing it (they need now to refer to the latest version).
A further step is to publish the data. In this case the version is frozen: no more edits allowed. Any other change would be against a new version of the data set.
I initially thought of using a VCS (e.g. Git) but the "2-step" commit complicates things a bit (not that they were that easy before). Also, I don't know of any VCS that stores the "work-in-progress".
Is there any tool, framework or library that implements this or something similar?
Related
I have read multiple posts about this but do not have a clear answer yet.
We are transitioning to EF Core 2.0 company-wide, one project at a time.
The challenge is this:
A new project starts and a database is created using code first, migrations etc.
another programmer needs to create a project targeting the same database.
This programmer can use Scaffold-DbContext and generate current models.
However, a new column is needed and this second programmer adds it.
Now...how do we best update the other projects?
Is there something that checks and syncs or shows what is out of sync between your model and a database? Meaning check the database for changes...not the model.
We don't mind buying a tool if that is the best solution.
The solution we have been using, very successfully is the Database project in Visual Studio.
Using that each developer has the project in their solution, changes are made against it locally.
Then we can do a "Schema Compare" inside of VS.
We have been using this successfully for 4 of us the past three weeks extensively with almost no issues.
This has even worked for keeping versions and changes to our stored procedures current.
It works well with VSTS also.
Here are some of the posts I read that helped me understand it:
https://www.sqlchick.com/entries/2016/1/10/why-you-should-use-a-ssdt-database-project-for-your-data-warehouse
https://weblogs.asp.net/gunnarpeipman/using-visual-studio-database-projects-in-real-life
..and this forum had a lot of relevant questions/answers:
https://social.msdn.microsoft.com/Forums/sqlserver/en-us/home?forum=ssdt
We have recently begun using Entity Framework for accessing all the various databases we touch on a regular basis. We've established a collection of library projects, one for each of these. For many of them, we're accessing established databases that do not change, and using DB first works just great.
For some projects, though, we're developing evolving databases that are having new fields and tables added periodically. Because these libraries are used by multiple projects (at the moment, just two, but eventually many more), running a migration on the production database necessitates a republish of both/all sites that use that particular DB's library. Failure to update the library on any other site of course produces the error that the model backing the context has changed.
How can we effectively manage the deployment/update of the Code-First libraries to all of the sites that use them each time a change to the database is made?
A year later, here's what we came up with and have been using.
We now include the following line in the Application_Start() method:
Database.SetInitializer<EFLib.MyHousing.MyHousingMVCContext>(null);
This causes it not to throw a fit if the current database model doesn't exactly match what's in the code. While there is still potential for problems if non-backward-compatible changes are made, this allows for new functionality to be added without the need to re-deploy every site that uses these libraries when the affecting changes are not relevant to that particular site.
I have a web application that allows the clerk to edit information. I copy an entity before the edit starts in case the user decides to cancel the changes. The problem is that any changes made on the copy is applied to the original object.
In C# I would create a deep copy to avoid that issue, but this application is using Entity Framework... I am not sure how to do a deep copy of an entity.
Here is more details on my problem... I am still trying to resolve.
I have a xaml screen with a grid binded to a list of inventory items. The items are an EntitySet. When I want the user edits one of the items, I copy the values of the current entity in an object "EntityToEdit" of the same type. The user makes a change, saves, and the list is automatically refreshed with the changes.
The problem occurs when the user selects another item to edit. That second item is somehow changed with the changes made on the first item....
How can I break the "link"?!?
There is a lot of stuff going on in your question, I barely know where to start.
First, I do not recommend binding the Entities directly to your UI. Instead I recommend using some form of abstraction between your database and the presentation layer of your application.
There are a couple of established best-practices patterns that could be used, mostly depending on what technical environment you are working in. Have a look at the MVC pattern (Model-View-Controller) or at MVVM (Model-View-ViewModel).
Regarding your deep copy, there are lots of possible solutions. You can find some here at StackOverflow, like this one: "How do you do a deep copy an object in .Net?", where the objects are serialized into a Stream. Altough I, too, was using this kind of deep copy, but I try to avoid this. I see only very rare cases where a deep copy is really needed.
You should also consider implementing the [IEditableObject][4] interface on those objects that you want to edit. This allows you for an easy way to structure when and how your edited values are commited and to optionally or cancel or reset your edits by implementing straight forward methods like BeginEdit(), EndEdit(), and CancelEdit(). Some .NET controls that support this interface out-of-the-box and call these interface methods automatically if they exist on your objects.
By implementing IEditableObject you have full control of how the values are committed to which object. This will help you to avoid changing the original object accidentally.
I'm looking on Entity Framework at the moment and working with Code First example. So far I can see that the framework does not handle model changes easily: whenever I want to add another field to a class/table, framework drops the entire database and creates it from the scratch.
Similar behaviour I saw in (N)Hibernate. (I could be wrong here, it was long time ago)
That is ok, as long as I work on tutorial. When a real-life project is involved, you can't afford to drop a database every time you need a new field in a table.
Just imagine scenario, you are working on a project with many clients. Every client has their own database. In release 1.0.1 I need to add a new field to one of the tables. If I drop database in my dev environment - not a big deal. (Still, I need to run a script to populate test data every time DB is dropped, and sometimes even this is no viable)
But what do I do when I need to deploy this new version? Make a SQL script to update client's databases without dropping them? then deploy binaries?
But how is this better than doing database mods separate from code changes?
(sorry for my bad english)
This is exactly why Code First Migrations exists. Take a look here (automatic migrations) and here (code-based migrations)
Our product has to be interfaced with multiple client/partner systems. For example, when a person is added/updated we have to notify changes to a 3rd-party system, for example by calling a web service or creating a xml file in a folder, etc.
We need a "hook" after SaveChanges has successfully persisted changes in the database.
Lots of information can be found about how to execute business logic when saving changes (before changes are persisted in the database), but less about executing logic after changes are persisted.
After investigating, I think to use the following:
// Persist data
cxt.SaveChanges(false);
// TODO: execute business logic that can get data changes
// Discard changes and set entities as unmodified
ctx.AcceptAllChanges();
Does anyone have a better solution for this scenario?
I know this question is a bit old, but figure that I would add this for anyone else searching on this topic.
I would recommend checking out EFHooks. The official version is a bit stale (e.g. .NET 4 only), but I have forked the project and published a new NuGet package, VisoftInc.EFHooks.
You can read more about both packages in this post: http://blogs.visoftinc.com/2013/05/27/hooking-into-ef-with-efhooks/
Basically, EFHooks will let you hook into EF (e.g. PostInsert or PostUpdate) and run some code. The hooks are Pre/Post for Insert/Update/Delete.
One thing to note is that this is based on the DbContext, so if you are still using the ObjectContext for EF, this solution won't work for you.
You can override savechanges, then do the updating of the 3rd party systems at the same time as you save the data to the database.
see: http://thedatafarm.com/blog/data-access/objectcontext-savechanges-is-now-virtual-overridable-in-ef4/