Today Scott “The Dot Glue” is hyping AND spinning EF CTP 5 on his blog. Instead of my personal rants about how bad EF is, let’s take DOT Glue’s own example code and rip it to shreds with” code first” and see the future and the past to determine if we are really better off by comparing the EF to VFP.
After all “Source Code Doesn’t Lie”…
The Entity Framework CTP 5
Step 1 Model Class and DB Context classes.
Actually we have to build up the tables first, just a minor detail...
So let’s see what these model classes look like...
Public class Product
{
Public int productid { get; set }
Public int categoryid { get; set }
Public string productname { get; set }
// WTF is up with the ? talk about an after thought…
Public Decimal? UnitPrice { get; set }
Public Bool Discontinued { get; set }
public virtual category category { get; set }
}
Now we have to jerk with the category table class
Public class category
{
Public int categoryid { get; set }
Public string categoryname { get; set }
Public string description { get; set }
public virtual ICollection
}
Public virtual is one of the bullshit commands that Microsoft loves since it struggles to do anything that is straightforward in Visual Studio. Basically we have two tables or collections, products and categories and there is a relationship between the two that is lazy loaded so EF needs a property for some screwy reason.
Now we need to create a data context – No I am not shitting you
Public class northwind : dbcontext
{
public dbset Product {get;set;}
public dbset Category {get;set;}
}
Side note: You need less then and greater than signs around the table names. The blogger editor stripped them out.
Thanks goodness THE GLUE doesn’t use any real world examples I would spend hours hand coding the classes.
Step 2 Configure the connection string
No big surprise a connection string in the app.config, if vfp was accessing a sql database we issue a sqlconnect no biggie either way.
Security=SSPI;AttachDBFilename=|DataDirectory|\northwind.mdf;
User Instance=true"
providerName="System.Data.SqlClient" />
Step 3 Pull a collection from EF
DOT GLUE used linq in this example
Personally I hate the linq syntax it is sort of like reinventing the wheel and building a CUBE instead of a circle.
NorthWind Northwindw() = new NorthWind();
var p = from p in northwind.products
where p.category.categoryname = “Beverages”
select p;
Step 4 an EF update
NorthWind Northwind() = new NorthWind();
Product.Product = northwind.products.find(1);
Product.UnitPrice = 2.33m;
Product.Discontinued = false;
// this is where the VS Cheerleaders have an orgasm and praise gu!
Northwind.SaveChanges();
NOW Let’s run through this same example in VFP
Yes no class code or data context required.
For humor sake if we wanted a record object it would look something like this.
Select product
Scatter name oProduct blank
Select category
Scatter name oCategory blank
Yes that is correct in Visual FoxPro we use two lines of code no cryptic syntax no virtual relationship command. Even more important if the base tables structure changes no change is required to the code.
Next The Glue built a collection using linq… a cursor will work just fine in vfp.
Select * from products
inner join category on products.categoryid = category.categoryid
where categoryname = “Beverages” into cursor p
That looks familiar Native SQL in VFP sweet! Yes the same syntax can be executed against a sql database using sqlexec. This is important makes sure you read my closing comments with regards to sql commands and ef!
Perform an Update
Select product
= seek(1) // This finds a record
Don’t even complain about what happens if the record is not found THE GLUE didn’t handle that case either. Yes we could add the workarea and index into the seek command and use one line of code instead of two.
Scatter name oProduct
oProduct.UnitPrice = 2.33m;
oProduct.Discontinued = false;
// this is where VFP developers should be laughing at EF as we have been saving records with one line of code since foxpro for dos.
Gather name oProduct
Is it just me or are you having a WTF moment as well.
Folks Microsoft killed Visual FoxPro and now we have the entity framework don’t you love progress Microsoft Visual Studio style!
EF Validation
Next THE GLUE blogs about two property attributes: range and required. My first instinct when I read this was that is pretty cool. Then I sat back any thought about it for a minute and SAID this is bullshit and here is why.
If there is a UI involved the range and required validation should occur in the form! Now you are thinking yeah Mark you missed the case of a web-service and reusability. Fair enough but wouldn’t it be better to catch this in the business layer object of the middle tier and avoid a round trip to the server and connecting to the datasource etc- after all you die hard performance based MVC guys are worried about view state in the HTML being downloaded from the server slowing things down you should crap yourself if you have to load the EF data context when it isn’t needed.
Ironically in the next paragraph THE GLUE discusses Keeping things DRY
The “DRY Principle” stands for “Do Not Repeat Yourself".
Don’t you wish Microsoft came up with this idea with regards to the .NET FRAMEWORK that is all redundancy or how about 6 different Datagrids and list views and isn’t there two different View Engines for MVC. Better yet how about all the different flavors of Vista ... What an Joke!
The GLUE wraps this post up by getting us excited about other improvements
1) EF Code First CTP5 exposes a new set of change tracking information that enables you to access Original, Current & Stored values, and State (e.g. Added, Unchanged, Modified, Deleted). This support is useful in a variety of scenarios.
Well this sounds like VFP GetNextModified and GetFldState to me nothing new there.
2) EF Code First CTP5 now allows raw SQL queries and commands (including SPROCs) to be executed via the SqlQuery and SqlCommand methods exposed off of the DbContext.Database property. The results of these method calls can be materialized into object instances that can be optionally change-tracked by the DbContext. This is useful for a variety of advanced scenarios.
By advanced scenarios does THE GLUE mean cases that Linq to entities can’t handle or does linq to entities perform so poorly under some instances you really should use an SPROC? Regardless this was available years ago in VFP in the native VFP SQL statement and does SQLEXEC sound familiar to VFP developers?
THE GLUE writes in conclusion, EF Code First provides an elegant and powerful way to work with data [OBVIOUSLY HE NEVER USED A DATA CENTRIC LANGUAGE BEFORE LIKE VFP]. I [“THE GLUE”] really like it because it is extremely clean and supports best practices [IS THERE REALLY SUCH A THING AT MICROSOFT AS BEST PRACTICES WHEN TECHNOLOGY AND PARADIGMS ALMOST CHANGE DAILY?], while also enabling solutions to be implemented very, very rapidly [THIS HAS TO BE A TYPO]. The code-only [HE NAILED THAT ONE THERE IS A TON OF CODE IN VISUAL STUDIO APPLICATION DEVELOPMENT AND NOT MUCH RAD] approach of the library means that model layers end up being flexible and easy to customize….. [OH REALLY WE MAKE A CHANGE TO A DATA STRUCTURE AND LETS SEE WE NEED TO RECOMPILE THE EF AND CHANGE ALL THE CLASSES REAL FREAKING EASY]
I think it would behoove THE GLUE to use VFP for a while before writing a blog post as according to my blog post there is nothing new or elegant based on the sample code Scott GU is blogging about when using the Entity Framework. Obviously VFP should be the winner but even if you want to give the THE GLUE the benefit of the doubt, look there is not a thing in this example that is new or different at best this example is on par with VFP technology.
The funny part is the GU is getting praised on his blog for EF when it is doing nothing but causing more work and I am sure a few cheerleaders will flame me for pointing out just how bad the new EF technology really is…. And here I am trying to make their jobs easier by suggesting Microsoft builds (for you anyway - I went open source for all new web projects) development tools that work!
When all is said and done aren’t you glad source code doesn’t lie!
Until Next Time
Happy Coding!
.Mark
By the way in case you are interested here is the link to the no confidence petition for the entity framework started by several MVPS.
http://msmvps.com/blogs/peterritchie/archive/2008/06/25/entity-framework-petition-of-vote-of-non-confidence.aspx

