Skip to main content

Posts

Showing posts with the label Tricks

Entity Framework - Suporte para múltiplas BD

Uma das limitações que o EF ainda tem é o seu suporte para múltiplas bases de dados. A opção simplesmente não é suportada à partida, apesar de atrair muitos votos no UserVoice . Antes de mais, quero dar crédito aos autores da ideia original. Esta ideia não  foi minha. Eu fiz download do script original, utilizei-o e depois reescrevi-o quase  completamente para lidar com alguns casos adicionais. Eu adoro Linq, por isso aproveitei para utilizar bastante na nova versão. O script original pode ser encontrado aqui . O truque é fazer o EF acreditar que todas as tabelas/objectos mapeados estão na mesma base de dados utilizando sinónimos. Eu não sei se todas as BD's suportadas pelo EF têm alguma feature semalhante a sinónimos, mas isto é certamente possível no SQL Server (que eu estou actualmente a usar). O meu script acrescenta a possibilidade de primeiro fazer backup dos ficheiros edmx  (algo que eu acho útil para evitar que o meu modelo fique corrompido), substitui os pr...

Entity Framework–Multiple Database support

One of the limitations that EF still has is the support for multiple databases. It simply does not support that option.out of the box. Allthough on UserVoice a lot of people have voted this feature, it still doesn’t exist. Credit where credit is due: this is not my original idea. I first downloaded the original script, used it and then i almost completely rewrote it to suit my needs and to improve certain aspects. I love Linq, so i used it a lot. The original and fantastic script can be found here The trick is making EF believe that those objects are in the database by using synonyms. I dont know if every EF-supported database allows synonyms or not, but this is possible in SQL Server (which i am using).   My script adds the ability to backup the edmx files first (which i find useful to prevent my model from being corrupted), replace the prefixes in the resulting model (which i had to do by hand with the other script) and finally reorder the navigation properties by al...

C# 2.0 - Anonymous Methods

Let's get back to my series on the evolution of the C# language. Everybody is familiar with what a delegate is, right? We know that delegates are a big part of the events system in .Net. We register a method on that delegate ( called handler ) that is invoked when the delegate is invoked. In other words, a delegate simply points to a method. The advantage here is that, as the delegate wraps the method and is an object, we can simply pass it to a method as a parameter! Think about this: This is how we use a delegate in events, for instance. The class exposes the delegate and we register our method to listen it. When the delegate is called inside the class, all the registered methods will be called (because of this, we call it a Multicast Delegate ). This works well, because all you have to do is have a method with the required signature. In the example above, we needed to declare two methods just to make a Console.WriteLine. Isn't this too much? I think it's a bit to...