Question

Similar to how linqpad works, when you just add a database connection, and you automatically get a context to work with.

enter image description here

I did literally like 0 work to get this context. How do I do something similar in my code?

Say for one of my projects I just want to load up a database

 var dataContext = new DataContext(myConnection);

//linqpad has like a typed context, which i'd like
var customer = dataContext.Customers.Where(x => x.Id == 4);

without having to have the Customer class already created and mapped.

How does linqpad do this?

I'm guessing it's some sort of derived data context, but how does it handle the creating of all the models with the correct properties and what not?

Any pointers would be fabulous

Was it helpful?

Solution

LINQPad "cheats" and builds a typed data context behind the scenes using Reflection.Emit. It makes it appear as though something dynamic is going on, but really, it's not that different to adding a "LINQ to SQL Classes" item in like Visual Studio (or using SqlMetal as Stephen suggests).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top