Any ideas on how SqlWorkflowInstanceStore could be made to work with a different schema? - workflow

Thank you in advance for any input or advice!
I am using WF4 in a large project that requires long running workflow persistence. For deployment consistency reasons, it would be helpful to use an alternate schema name for instance store objects. For example, System.Activities.DurableInstancing.InstanceTable would become DurableInstancing.InstanceTable, etc.
Updating the SQL scripts to accomplish this server-side is not difficult, but from what I can tell, there is no way to modify the default schema used by SqlWorkflowInstanceStore when generating commands. It appears the schema name is read from SqlWorkflowInstanceStoreConstants.DefaultSchema, which is a constant (as the name implies). SqlWorkflowInstanceStore is sealed and it seems to be quite a task to roll your own InstanceStore, so I'm reluctant to pursue that option.
Does anyone know of simpler way to do this that I might be missing? Also, I am aware that changing the schema name would add steps to applying future instance store updates, but can anyone foresee other potential problems?

I never tried that but as far as I know the SqlWorkflowInstanceStore only calls stored procedures. All of those need to be in the System.Activities.DurableInstancing DB scheme but you should be able to move the tables and views to another DB scheme.
Quite frankly I would probably never do that as that would mean I am no longer in a supported scenario and see no benefit. If for some reason you need to query the System.Activities.DurableInstancing tabled/view and because the way your app is set up that needs to be in another DB scheme I would just create views there pointing to the original tables.

Implementing your own InstanceStore isn't difficult. We were in a similar situation, we really didn't want to have two schemas, ours and the Workflow SQL. We implemented our own InstanceStore and it's about 280 lines of code. We had to add one table to our schema and a couple fields to existing tables.
I would encourage you to try implementing your own InstanceStore.

Related

Entity Framework code first - development strategies

Working on a brand new project from the ground up. That means the data model is in a constant flux, doubly so because things are, inevitably, not as well planned as they should be. Model classes are being created and changed fairly regularly.
The plan was to use the latest version of EF with all the neat code-first stuff in it. But we're constantly tripping over the limitations the framework has in terms of adding or updating tables. The initialization options seem to allow only the complete deletion and re-creation of the database, which isn't really ideal.
I've had a look at the migrations. But this seems a sledgehammer to crack a nut: we don't need to detail every single small change and update with a new migration scaffold.
Are there some better strategies to deal with this? For instance, I started writing some unit tests to pre-populate one of the contexts with some test data, but because this causes the whole Db to drop and re-create, it causes problems with all the other contexts. Or perhaps making use of a custom initialiser to seed the data for us? How can we easily exclude these in production code?
We're also wondering about perhaps abandoning code-first and going back to EDMX diagrams. At least that way changes result in updated SQL commands which can be run directly against the database.
Any suggestions gratefully received.
I think, imho, that:
as the database schema must at least match your model you should/must detail every single change, and code first migration allows that and trace the changes over time
code first migration also allows to migrate the database schema for you
code first migration also allows you to produce sql that allows you to migrate the schema
For these reasons code first is as good (if not better) as the edmx approach
Please take few minutes to implement http://msdn.microsoft.com/en-us/data/jj591621.aspx
One other point, always imho and in a perfect world, if you unit test the business of you model you should not need the DAL, use generic collection. Be aware of different comportement of linq to object vs linq to entities, for example concerning the case sensitivity.

entity framework and database default values workaround

I have to decide about an important item and I need your help.
I'm facing an huge existing database with a lot of default values on nullable columns.
The team has to build a new MVC4 application on top of it (in fact it is a rewrite of old VB6 application).
I (as a consultant) have 'forced' the use of EF5 to get rid of all stored procedures and migrate to a more modern techology.
Now, after my research, it is clear to me that EF5 doesn't support database default values per default. This is why my inserted records are corrupt (they are inserted because the columns are nullable, but with NULL of course).
Some options came up like using the constructor technique, setting the default values in design on the edmx, or playing around with the xml of the edmx.
Despite, these methods are not usefull for us. Where the constructor technique looks ok for me, it is not feasible to do that for all tables in the DB. I also have a 'njet' from the technical person because he wants to maintain these values on 1 place. Same story for setting the default values in design. The database is also not in our scope (read: as less as possible changes to keep existing applications running).
At this point, I'm not sure it EF is the correct choice for our project.
Is somebody aware of (3th party) tools that can fill in the database default values automatically in the generated xml of the edmx file?
Is there som more info about how this xml is build and if there is a possiblity to interfere in the process?
Is there a good readon why these default values should not be taken? Is this going to change in a later release?
Are there other good practices that can be applied to that problem without having all values dupplicated or a massive workload?
Can I arrange something with my poco generator?
I realize there are already a lot of posts of this topic. Too bad, there is no suitable solution for me since we have already something existing and (with all respect) an old VB6 team that I have to convince.
Thanks for your feedback!

Can a project have two different EF data models that reference the same table?

I've system that has a primary data model to perform most of the work.
The model has quite a few tables and with performance in mind when I came to add an administrative feature to the application I decided to use a second separate data model.
All works well until my second data model needs to access a table that is also in the primary data model. Now, from digging around I can see this can cause problems.
The two possible workaround I've come up with are to either:
Put the data models in separate projects.
Use views / stored procedures for accessing the table in question when required.
Method 1 seems the simpliest but I'm concerned about whether there would be any performance loss. Method 2 seems a bit messy and takes the point out of using EF.
Before I plump for using method 1, is there an easier work around that I could use?
In the end I decided to put the two data models into separate projects and I've there hasn't been any slowdown that I've been able to notice (I've not done any benchmarking but it's passed the perception test).
In one of her online tutorials EF guru Julie Lerman says that you should put your data model in a separate project anyway, so I don't think this has been a bad workaround.
I am working with 2 models in the same project, because I connect to 2 different databases. I have put different namespaces using "Custom Tool Namespace" on *.tt files but it is not necessary. It generally works, but it cannot handle situation when the entity (table) with the same name is in both models. When you save one model the entity with the same name is deleted from the second model.

How do I add relationships at runtime using DBIx::Class and Catalyst?

In the application I am building, users can specify relationships between tables.
Since I only determine this at runtime, I can't specify has_many or belongs_to relationships in the schema modules for startup.
So given two tables; system and place, I would like to add the relationship to join records between them.
I have part of the solution below:
$rs = $c->model('DB::system')->result_source;
$rs->add_relationship('locations','DB::place',{'foreign.fk0' => 'self.id'});
So the column fk0 would be the foreign key mapping to the location primary key id.
I know there must be a re-registration to allow future access to the relationship but I can't figure it out.
I don't believe you can re-define these relationships after an application is already running. At least not without discarding any existing DBIC objects, and re-creating them all from scratch. At that point, it would be easier to just re-start your application, I suspect.
If you're content defining these things dynamically at compile time, that is possible... we do something similar in one of our applications.
If that would be useful to you, I can provide some sample code.
The DBIx::Class::ResultSet::View module might provide a rough approximation of what you're looking for, by letting you execute arbitrary code, but retrieving the results as DBIx objects.
My general opinion on things like this, is that any abstraction layer (and an ORM is an abstraction layer), is intended to make life easier. When it gets in the way of making your application do what it wants, it's no longer making life easier, and ought to be discarded (for that specific use--not necessarily for every use). For this reason, I would suggest using DBI, as you suggested in one of your comments. I suspect it will make your life much easier in this case.
I've done this by calling the appropriate methods on the relevant result sources, e.g. $resultset->result_source-><relationship method>. It does work even in an active application.

Entity Framework - Autogeneration vs Manually Creation of Entities

You can refer to this post of mine.
It is not the case that, someone will be able to generate perfect Entities from the database tables every time.
If any entity is not looking perfect, how can I tweak it? And how should I tweak it?
Or should I search for the flaw of relations in the Database, fix it first and then try to generate Entities over and over again until the desired entity is generated?
But it may happen that, I would become unable to figure out the reason of an imperfect entity being generated only looking at the database tables.
You should clarify your requirements: should be everything created automatically from database or you allow hand-made updates or will do it manually (once automatically then only manual updates).
Remember, that from time to time after db structure changes you will have to generate entities again, then you may lost your changes. Consider using partial classes, so some of your code changes could be persistent across automatic generations.
But in your case let's try to figure out how how the MS has prepared the demo database and try to follow the rules. Maybe you would like to read some database design guides?
More details about your environment could help with more precise answer.
We generate the EF model based on the database. When the database changes we refresh the model. This works fine for us.
It is only if you are doing something special that you need to tweek your entities.