I’m not working a lot with emails and email addresses. Loading, parsing and so on. Because of that I’m also using a
MailAddress class a lot. And I hate it. The class, even simple, makes ma wanna scream.
Consider simple code like this.
var mail1 = new MailAddress("firstname.lastname@example.org");
var mail2 = new MailAddress("MAIL@example.com");
What do you think the output be? Drum roll please…
Why the hell somebody did the work overriding the
Equals and not providing the implementation of
GetHashCode that aligns with that?
I think it’s time to implement my own.
Few weeks back I was hired to debug the above mentioned problem. The application was using Entity Framework 5 and Firebird as a database. The message says clearly what’s wrong. How hard can it be to fix the problem, right? I was thinking the same. And how wrong I was.
After receiving the sources and quickly extracting the bare metal with error I started looking for
Image type. And there it was. The class had
Id property, so the key was kind of there. The default convention should be able to find. Let’s look at configuration, if there’s something suspicious. And even better, the
HasKey call is there. OK, maybe there’s some magic in
OnModelCreating. Nope. Everything as expected.
By the way, the exact error was:
ModelValidationException: One or more validation errors were detected during model generation:
System.Data.Entity.Edm.EdmEntityType: : EntityType 'Image' has no key defined. Define the key for this EntityType.
System.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Images' is based on type 'Image' that has no keys defined.
That’s the overview. Next two hours I was trying to changes something here and there and make it fail, so I knew it’s doing what it should do. Removing some stuff that might break something. And still no luck. But then something caught my eye. The error is talking about “Image” type. My type from database is “IMAGE” (the mapping and classes were generated by Entity Framework Power Tools and because of how SQL standard defines non-quoted names, it’s by default all uppercase). Yes, there was a property added to some entity that had a type
Image, I mean
So quickly adding
OnModelCreating and I was done. Lesson learned.
Because on the server where the application is running I’m unable to do backup using regular
gbak tool, I turned my attention to
nbackup. In fact I’m doing just file copy and using
alter database begin backup/
alter database end backup (more info). But doing the backup is only part of the story. You have also know you know how to restore it and whether you can restore it (aka whether the backup is not corrupted).
If you have done backup using process described above and you have the file, you can’t just start using it. You need to “restore it”, which means changing a flag inside the database file. This is where the
-F switch comes to play. But no matter what I was doing, I was getting:
Failure: Error (5) opening database file: <some>.fdb
This error message is, well, completely utterly useless. While waiting for reply from firebird-support list I played with Process Monitor (of course) to see what’s wrong. But I haven’t seen any disk activity with errors. Changing paths, trying invalid paths (this produced just error 3), running 32bit version of
nbackup, using one from Firebird 2.1, … And nothing. Then I decided to have a look into the ultimate documentation aka sources. It was not difficult to find piece of code:
b_error::raise(uSvc, "Error (%d) opening database file: %s", GetLastError(), dbname.c_str());
GetLastError. Time to jump into System Error Codes. And in no time I know it’s
ERROR_ACCESS_DENIED. That’s a progress. Quick check of permissions and yes; the user I was running under had no permission to write to the file. Quickly changing that and everything was working fine.
Hope it helps somebody.
If you’re watching at least a little what’s going on in .NET world you noticed that Entity Framework is open source for couple months now. That’s a good thing.
On the other had there are still some pieces of the complete stack that are not open source. The one that I’m missing most is SqlClient. I’m writing provider for Firebird and although the MSDN documentation contains a lot of content sometimes it’s not obvious how things should behave. Then the
SqlClient‘s behavior is (at least by me) taken as de-facto standard. I think everybody expects behavior that adheres to it. Over the last few years I’ve spent maybe thousands of hours trying things with
ILDASM-ing into internals. But it could be easier if you could just step into the code and see the behavior, variables, … Also other people might look at the code and instead of just reporting bug posting also related info where to find the proper implementation or how it differs internally.
I wish to go back to this post in the foreseeable future and happily strike-though the complete text and just say that SqlClient is open source too.
When the Entity Framework was first introduced it came with (basically) two flavors of querying. LINQ to Entities and Entity SQL (ESQL). I’m not going to describe the history here. But as LINQ (any LINQ) gained popularity it was obvious, that LINQ to Entities is a future (though it had initially small deficit compared to Entity SQL). I even on my courses recommend using LINQ to Entities whenever possible and I’m also showing some dynamic querying where normally people start concatenating ESQL strings.
Recently Entity Framework added support for spatial data (and enums, …). All examples I’ve ever seen around spatial data and Entity Framework were using LINQ (obviously). Only few weeks ago I realized how the querying should work with Entity SQL. As it turned out spatial data are not first class citizen in Entity SQL world. There’s no literal for i.e. point. But canonical functions are here to save you. There’s a bunch of these spatial data related. What we’re looking for though is
GeographyFromText. With these you can construct
Geography datatype from well-known text (WKT) and use it in query.
I’ve never used Entity SQL query in any real-world application I created. I’m too afraid of typos etc. But I did a lot of magic with expression trees. Wondering what percentage of people using Entity Framework is using Entity SQL and whether there are some scenarios that are crazy hard in LINQ…