Dailycode.info

Short solution for short problems

EF: add new Migration when there are multiple migration configurations in your project

You could get this error:

More than one migrations configuration type 'Configuration' was found in the assembly 'BDO.DataLayer.MSSQL'. Specify the fully qualified name of the one to use.

And then look for a while for a solution. You'll find that you need to tell what configuratin type to use, but not so easy to find is how to get this name.

So extend the add-migration command like this (Verbose parameter shows the SQL that is being rendered):

Add-Migration "VATRefundExtraProperties" -ConfigurationTypeName "DataLayer.MSSQL.Migrations.Configuration" -Force –Verbose
And after update the database:

update-database -ConfigurationTypeName "DataLayer.MSSQL.Migrations.Configuration" -Force
You can also add the Verbose flag here if you like.

More info on the attributes for ?-migration:


.Net Self Tracking Entities and JSON

I know this is not the ideal situation, better to use POCO's or simple objects. But when you already have a large structure in place using these self tracking entities, it can be usefull to do this. In my case we had webservices build for .Net client applications. Now we want to reuse this architecture and consume it on IPAD and IPHONE. Can this work? Yes offcourse. First I tried with SOAP, which works well, but there were 3 problems. First I was relying on a object generator tool of SUDZC, second SOAP is taking more data then JSON, third, iOS doesn't support nillable and the generator converted it into simple types. So now I decided to use JSON, use my own objects and reuse the webservices. 

This involved making a new service layer the consumes the same process layer as my .Net WCF services.

But you will notice that when you try to pass a self tracking entity in JSON format, the webservice will crash. 

The solution to this was:

Set the IsReference flag to false, and remove the DataMember attribute from the ChangeTracker.

//[DataMember]

public ObjectChangeTracker ChangeTracker

and

[DataContract(IsReference = false)]

public partial class GP_MES_Clients: IObjectWithChangeTracker, INotifyPropertyChanged

After this all is changed, you can consume this service in your iOS application!


Entity frame work with self tracking entities database changes.

There are many ways to implement the entity framework. I chose to add a link to the template in a different project. This implementation can be found here.

Now this article doesn’t describe in detail what to do when the database changes. This can be a very pain full operation if you have to figure it out yourself. That’s why I wrote this post.

In this case I chose to change an existing database table and add a new table to the database.

I divided the solution in 3 layers, the database layer, a process layer and a GUI. In between you’ll find the entities going from top to bottom. So no use of DTO or any kind of mapping.

 

The edmx model is located in the DataLayer. Also the generated tt files for the self-tracking entities is located there.  In the CMDBProcessLayer  I added the context as link and in the CMDBentities project I added the self-tracking objects as linked files.

Let go to the update process.

Step 1 is off course update the database.

Step 2: Remove the linked files to the entities and the context.

 

 

Step 3: Update the database model (edmx file)

before:

 

after: (Do not forget to save the model, else you could get problems creating the code generation items)

 

Step 4: Delete the old tt files in the DataLayer

 

Step 5: Generate new tt files

 

Step 6: Turn off code generation on the entity model.tt file

 

Step 7: Delete all remaining file generated by the entities.tt file:

 

Stap 8: repeat step 6 and 7 for the context.tt file

 

Step 9: Add the Entity.tt file to your entities shared projects. (If you do not see the files, choose 'All Files' in the file filter box on your lower right.)

 

Step 10: add the context.tt file as linked file to the process layer

 

Step 11 (Last step): Change the namespace of context.tt file to the namespace of the shared entities project.

 

Now you can build and you have the new data structure.

 


The object cannot be deleted because it was not found in the ObjectStateManager

I was trying to delete an (state tracking) entity from the database. But when I called the deleteobject method, I got this nice error: The object cannot be deleted because it was not found in the ObjectStateManagerDescription: http://i1.social.s-msft.com/Forums/resources/images/trans.gif?cver=1864.689%0d%0a.

So after some googling around, I found this solution:

In stead of just deleting the object like this:

ents.TrialType.DeleteObject(type);

ents.SaveChanges();

 

I now first attacht the object and delete it afterwards:

ents.TrialType.Attach(type);

ents.TrialType.DeleteObject(type);

ents.SaveChanges();

 

Off course you can also just pass the ID of the object, retrieve the object from the entity context and delete it:

public void RemoveTrialType(string key)

{

    TrialType t = GetTrialTypeByID(key);

    try

    {

        ents.TrialType.DeleteObject(t);

        ents.SaveChanges();

    }

    catch (Exception)

    {

        throw new ArgumentException("Delete failed, not possible to delete record!");

    }

}