2

I need to compare what's on my POCO classes against my database. I am currently using Microsoft Roslyn and Microsoft.SqlServer.SMO and comparing the data, in my opinion, 'roughly'. It's working perfectly, but I was wondering if there was a better way to do so.

Today I have to compile the file using Roslyn, load the database info and compare what I want.

Let's say I have this POCO file:

[Table("XPTO", Schema="ASD")]
public class MyPoco 
{
    public int PropId {get; set;}
    public virtual Prop Prop {get; set;}
    public DateTime? MyDate {get; set;}
}

And I also have the Table info. I want to see if what is nullable in my POCO class (MyDate) is nullable on my database and vice-versa. I want to see if my database has a Foreign Key to table Props and if it is not-nullable as in my POCO.

As I said, it's already working today, I just wanted to rewrite the code to use a more effective way to even maintain.

Does EntityFramework provide some sort of API or anything?

System Info

SQL Server 2008 R2
Entity Framework 4.1
C# 4.0

Thanks();

eestein
  • 4,914
  • 8
  • 54
  • 93

1 Answers1

2

If I'm reading your question right - you'd want some API to fetch the relationshiops and other metadata information from the DbContext or EF/code-first at runtime?

The best I've seen so far - and the best the two of us could do was this...

How I can read EF DbContext metadata programmatically?

...take a look if this can help you...

And you're welcome to post there if you happen to come into possession of some new and vital piece of information (we made it with the intention to serve as a one-stop question for subjects like this).

Community
  • 1
  • 1
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
  • Thanks for your answer, that could be a place to start. That is basically what I need, except that I need to fetch the differences between the instantiated context, or even the POCO files themselves, as I can use Roslyn for that, and compare with the DB. I'll take a look at your answer and see if that works for me. Thanks again. – eestein Apr 04 '13 at 16:26
  • You're welcome. Well, this is the current 'runtime' snapshot - those should 'correspond' to the Db. But for a 'real Db' state - you'd need to query the Db - use the system table to fetch tables, relationships, fields etc. and build your `Left` model (Db) - then compare to the `Right` model (your code one). That's what I used a bit simplified with Db code generators, you go hierarchically and compare, something like that at least:) – NSGaga-mostly-inactive Apr 04 '13 at 16:44
  • I just took a deeper look into your approach, but seeing that you instantiate the context I decided not to go that way. The problem is that I check the poco files and using the attributes I compare with the information from the table. The application is pretty big and using this approach I would need to handle several custom DLLs that would change from time to time. Anyway, your answer got me thinking, so thank you again. – eestein Apr 05 '13 at 10:54
  • you're welcome - every case is specific, you know best (you just can't rely on the attributes if someone uses fluent configuration, like I do most of the time - and many things can't be done w/o it - if that's what you mean by attributes). You should still close the question - put your own answer if this isn't fitting enough. Cheers. – NSGaga-mostly-inactive Apr 05 '13 at 11:02