Skip to content

Troubleshooting Entity Framework Connection Strings

In an application which uses the Entity Framework, you may see the following error at runtime:

MetadataException: Unable to load the specified metadata resource

The cause of this error is a missing or malformed Entity Framework connection string. In particular, the cause of this error is the metadata parameter of the connection string. Let’s examine this more closely.

Which Config File?

Before you begin troubleshooting, make sure you are looking at the correct file.

Connection strings are loaded from the configuration file for the executing assembly, which may not be the assembly which contains your Entity Framework model. So if you have a data assembly containing an Entity Framework model and a web assembly containing a web application which references the data assembly, then the Entity Framework connection string needs to be in the Web.config file for the web assembly. The data assembly can have its own connection string, if you like (this is convenient, as it will be used by the Entity Framework designer), but it will not be used by the web application at runtime.

Metadata

MSDN documents EF connection strings generally. But the cause of the error above is specifically the metadata parameter. This parameter tells the Entity Framework where to find your EDMX at runtime. When your application is compiled, the EDMX is split into three parts: CSDL, MSL, and SSDL. For the purposes of this post, you don’t need to think about what they mean right now; it’s enough to know that you need all three of them at runtime.

The EDMX can be supplied to the application as embedded resources or files on disk. The default is embedded resources, so we’ll focus on that case.

The metadata parameter for an application with an Entity Framework model called Model.edmx in an assembly called Simple Mvc.Data.dll might look like this:

<connectionStrings>
	<add name="MyEntities" connectionString="metadata=
            res://Simple Mvc.Data.dll/Model.csdl|
            res://Simple Mvc.Data.dll/Model.ssdl|
            res://Simple Mvc.Data.dll/Model.msl;provider= <!-- ... -->

So you can see there is one reference for each of the three parts of the EDMX that we need at runtime. They all work in the same way, so let’s examine just the first more closely. The CSDL reference looks like this:

            res://Simple Mvc.Data.dll/Model.csdl

It specifies three things:

  1. We’re loading the CSDL from a resource. That’s the "res://" part.
  2. The name of the assembly which contains the resource, "Simple Mvc.Data.dll". If your assembly is strong named, you can specify a strong name, in all its verbose glory, here.
  3. The name of the resource itself, "Model.csdl". Do not confuse this with the EDMX or model name. In this case they happen to be the same, except for the extension, but that’s not always true!

Let’s examine #2 and 3 more closely.

Assembly name

It’s really common to omit the assembly name in a connect string, and use * instead, like this:

<connectionStrings>
	<add name="MyEntities" connectionString="metadata=
            res://*/Model.csdl|
            res://*/Model.ssdl|
            res://*/Model.msl;provider= <!-- ... -->

This means that instead of loading the resource from one specific assembly, the EF will scan all loaded assemblies at runtime. Two obvious downsides to this are that it takes a bit of time to do this, and the assembly might not be loaded yet. The second case is one reason you might get the error at the start of this post. So specifying the assembly explicitly is always a good idea, I think.

Resource names

Remember how I said that the resource name isn’t necessarily the same as the model name? Here’s a real-life example of that. I opened one of our production assemblies in Reflector, and found this:

There’s actually a good reason that those resources have such bizarre names, but it’s a digression and not relevant to this post. The point is that after you’re sure the assembly name is right, the next step in solving the error at the top of this post is to double-check the resource names.

A simpler resource name will look like this:

Remember the "simpler" metadata where I used * instead of the assembly name? You can go even simpler. This metadata, perhaps surprisingly, actually works (with caveats):

<connectionStrings>
	<add name="MyEntities" connectionString="metadata=
            res://*/;provider= <!-- ... -->

This is the "throw everything against the wall and see what sticks" approach to a connect string. It will probably fail if your resources don’t happen to have the same name as your model, or if the assembly doesn’t happen to be loaded. But it (in simpler cases, anyway) frees the programmer from having to think about what any of this stuff means, so it’s a popular solution.

Tagged , ,

New InterBase Partner DVD In Progress

The next version of InterBase is nearing completion, and, as always, will include a Partner DVD with demo versions of third-party tools supporting InterBase. If you are an InterBase Partner, you should have already been notified of how to submit your product.

If you produce a tool or component supporting InterBase and have not already received a notice about the Partner DVD, then please contact Anders Ohlsson (firstname.lastname@embarcadero.com) immediately.

Tagged ,

grid.history Demo Fixed

Apologies to those who tried my grid.history demo page Friday. In the course of updating the integration to support jqGrid 3.7.2 and simultaneously learning GitHub’s pages feature, I killed the demo. It’s fixed now, and I’ve added the ability to run the unit tests directly from that site, making it easier for me to test multiple browsers on the demo site, instead of just locally.

Tagged , , , ,

grid.history: A New, Free Integration for jqGrid and jQuery BBQ

I recently finished an integration between the open source jqGrid and jQuery BBQ libraries. I have released my integration as open source, as well. As with both projects, it is dual-licensed under the MIT and GPL licenses.

jqGrid is a JavaScript grid component with many useful features. I have previously explained how to use it with ASP.NET MVC.

jQuery BBQ is a library which helps you manage proper functionality of the web browser’s back and forward buttons by changing the fragment (hash) of the URI. Using my integration means that you can have a jqGrid on a web page and the back and forward buttons will work as expected.

To be specific, when you first load a certain page, the grid will probably be on page 1. You could click the next page button on the grid to go to page 2. Without my integration, clicking the back button would not take you back to page 1. With my integration, it will. Similarly, I support the sort and records per page combo box. You can of course extend this yourself to track other grid features.

The image above shows a demo page I’ve prepared, which you try for yourself. The grid on the left does not use the history plug-in. The grid on the right does. Note that when you page, sort, or change the number of rows shown in the grid with the history, the URI fragment also changes.

I have contributed my plug-in back to the jqGrid project. You can get the source from github.

Tagged , , , ,

Replacing Controller.Session in ASP.NET MVC: Is This Wrong?

Here’s some code I wrote:

public class MyBaseController : Controller
{
    // ...
    public new ISession Session { get; private set; }
    // ....
}

ISession is an interface type I wrote which exposes everything I store in the session at runtime. I use constructor injection to assign this to a simple type which maps to the "real" Session during application runtime, and a "mock" session for unit testing. Session, in ASP.NET, is a user-specific cache.

This hides Controller.Session (of type HttpSessionStateBase) and replaces it with a strongly-typed, mockable replacement. Normally I wouldn’t use public new, but in this specific case it seems like the best way. It stops developers from using the Web-dependent Session property in derived types and forces them to code to the strong-typed interface. But still, it’s public new, which seems yucky.

What do you think?

Tagged , ,

Book Review: Rework

Rework, by Jason Fried and David Heinemeier Hansson, cannot accurately be described as the "sequel" to the first book to come out of 37 Signals, Getting Real. As a significant percentage of the book seems to be word for word identical to text in Getting Real, I think it’s more of a "remix." Getting Real focused on creating marketable web software, whereas Rework changes the focus ever so slightly to growing a business around marketable web software.

If you’ve read Getting Real (you can sample it online for free at the link above, and in my opinion it’s worth the time to do so), or if you’ve seen one of DHH’s presentations, then you probably have a good idea of what to expect in Rework. It’s short, to the point, quotable, and uses 37 Signals as the model for everything that is good about how to run a business. I don’t fault the authors for being proud of their company, but if you are looking for a dispassionate examination of when it makes sense to run a business in the way that they do versus other ways, you won’t find it here. You quickly get the idea that the authors look at other methods of running a business about as fondly as DHH looks at WS-*. Again, this isn’t necessarily a bad thing. It’s clearly worked out pretty well for 37 Signals. One can’t fail to notice, however, that 37 Signals’s products fit within a pretty narrow spectrum. Will their techniques translate to other markets and other industries? Perhaps; perhaps not. Rework doesn’t really answer that question.

Did I say quotable? It’s full of snappy, almost pithy zingers which are so obviously right that they would seem like tautologies if they were not so commonly ignored:

  • Workaholics aren’t heroes. They don’t save the day, they just use it up. The real hero is already home because she figured out a faster way to get things done.
  • You’re better off with a kick-ass half than a half-assed whole.
  • It’s also unfortunate that meetings are typically scheduled like TV shows. You set aside thirty minutes or an hour because that’s how scheduling software works (you’ll never see anyone schedule a seven-minute meeting with Outlook).

I don’t think these are empty words. I’m convinced the authors run their company this way. I’m also convinced it works for them. They took their own advice writing the book, as well:

We cut this book in half between the next-to-last and final drafts. From 57,000 words to about 27,000 words. Trust us, it’s better for it. So start chopping. Getting to great starts by cutting out stuff that’s merely good.

But I don’t agree with everything in the book. Sometimes the cuteness of the writing overtakes the point. It’s generally true that you should focus on the essentials. But they illustrate this with:

For example, if you’re opening a hot dog stand, you could worry about the condiments, the cart, the name, the decoration. But the first thing you should worry about is the hot dog. The hot dogs are the epicenter. Everything else is secondary.

Guys, next time you’re in Columbus, look me up, and I’ll buy you lunch at Dirty Franks. I respect what you have to say about business, but we have a thing or two to settle about hot dogs.

Indeed, later in the book the authors make this very point quite elegantly, with a more apropos example:

When we start designing something, we sketch out ideas with a big, thick Sharpie marker, instead of a ballpoint pen. Why? Pen points are too fine. They’re too high-resolution. They encourage you to worry about things that you shouldn’t worry about yet, like perfecting the shading or whether to use a dotted or dashed line. You end up focusing on things that should still be out of focus.

Avoid false precision.

The point here, after all, is not how to run a hot dog business in particular; it’s how to run your business. The best way to fail if that is to let details and directions steer you away from the ideas that only you can bring to the table. This is why "look inside yourself" can be a better approach to planning and business strategy than following an off-the-shelf formula. For most people, creating a new, free, application framework is the worst possible way to start a business selling web applications. I’ve seen many, many people fail at this. Except that for 37 Signals, it worked really well, perhaps because of the specific people who ran the company.

I strongly agree with the authors when they say:

Pour yourself into your product and everything around your product too: how you sell it, how you support it, how you explain it, and how you deliver it. Competitors can never copy the you in your product.

This is why it’s OK that Rework can be self-contradictory at times. In one chapter, the authors suggest that you "pick a fight." A good example of this is how Stack Overflow was to some degree a response to "hyphen-site." Only a few chapters later, however, the authors say:

Focus on competitors too much and you wind up diluting your own vision. Your chances of coming up with something fresh go way down when you keep feeding your brain other people’s ideas. You become reactionary instead of visionary. You wind up offering your competitor’s products with a different coat of paint. If you’re planning to build “the iPod killer” or “the next Pokemon,” you’re already dead. You’re allowing the competition to set the parameters.

Self-contradictory? Absolutely. Again, Rework is not a formula for how people with no interest in widgets can start a widget business. It’s written to provoke the reader, hopefully provoking them into keeping their business focused on what they can uniquely bring to the table.

Naturally, this presumes that the reader is, to some degree, interesting, and has ideas for things which could benefit other people. It’s not clear to me if everyone has good ideas, but many people don’t execute them, or if some people don’t execute good ideas because they don’t have them. What is clear is that having good ideas and not executing them is little better than not having them.

Tagged , , , , ,

Stir Trek 2: Iron Man Edition Wrap Up

Last Friday, I attended the Stir Trek conference here in Columbus. The day got off to an inauspicious start when I turned on my car. There was a high screaming noise, and acrid black smoke poured out from the engine. I opened the hood, pulled out a burning air-conditioner drive belt, and threw it into the bushes beside me. Now the engine ran fine! (Although, for some reason, this did not reassure my wife.)

The conference is held in a local movie theater. After a day of technical sessions, the attendees would be treated to a screening of Iron Man 2. I wasn’t able to stay long enough for that, but the technical content was more than worth the time and the measly $25 cost of the conference. The theater was still a great place to have a technical conference. The screens were huge and the seats were comfortable.

Keynote

Molly Holzschlag was scheduled to be the keynote speaker, but could not fly due to an ear infection and doctors orders. So she gave her keynote address, and a presentation on HTML 5 later on, via Skype, which was probably better than nothing, but only marginally so, given Internet bandwidth not really sufficient for real-time video. It at least provided some amusement when poorly-timed drop outs turned innocuous technical material into profanities. The HTML 5 presentation suffered from a lack of slides, which Molly made up for by asking us to imagine HTML DOCTYPEs and the like. I appreciate Molly going the extra mile to not leave the conference in the cold, but I didn’t get a lot out of these sessions, which is too bad, because HTML 5 is an ongoing concern for me right now.

Seeing Constraints, Kanban Explained

Generally, I rank conference presentations by whether or not I come out of them with useful information or ideas that I can put into practice back at work. The Kanban session did succeed in that respect. The speaker’s style was very conversational, almost off-the-cuff. He is a process consultant and related a lot of anecdotes about situations he has worked in.

One thing he emphasized which I do not see in much of the writing about Kanban is to use multiple boards when appropriate. For example, a development team can have "their own" board for non-functional changes, which might not ever appear on a board for company-wide items. You can go crazy with this, however; the speaker described one case where something like 36 boards were in use.

This session, like many presentations on development process, tended to presume that development throughput was a bottleneck in the company. This is often the case, but not always. What to do when development is not the bottleneck is a subject which could use fuller treatment.

Understanding User Experience Patterns

Since the original notion of a "design pattern" comes from Christopher Alexander’s writings on architecture (see Patterns of Software, by Richard P. Gabriel, for more info on this), it makes sense that software companies would want to apply design patterns to more than just code structure. Although there are a number of online user experience pattern catalogs, it still strikes me that this is an area where we, as an industry, have a lot to learn.

I very much enjoyed Ambrose Little’s presentation on UX patterns. He showed examples of good and bad interfaces, and discussed why they succeed or fail. He then tied those reasons into pattern catalogs, and demonstrated several of the online catalogs and discussed their strengths and weaknesses.

jQuery 1.4: What You Need to Know

For some reason, the lunchtime sessions were not in the Stir Trek catalog, so I will link Matt’s slides instead. Matt used a lightweight framework to display his slides using HTML and JavaScript instead of a tool like PowerPoint. This allowed him to demonstrate jQuery features right in the slides. It’s a very effective way of presenting his subject matter. There wasn’t a lot of new material in this presentation, given that jQuery 1.4 has been out for a while. It was a good overview, though.

Is Mobile Development Going to Follow the Book of Jobs (Steve That Is)?

This presentation was a lot like its title. Opinionated and verbose, but with an attempt to keep things fun the whole time. The presenter has been working in the mobile space for a long time, and has more than his share of war stories. Like a lot of people in mobile work, though, it seems to me that his opinions were colored by the specific nature of the work his company does. The speaker seemed to treat browser apps and web apps as the same thing. "Native" apps can be web apps. Browser apps can be local. Web app doesn’t necessarily mean "in the browser." HTML/JS isn’t necessarily a web app, especially in the mobile space. Overall, though, I enjoyed this a lot, though I’m not sure I’m going to take practical info home with me.

JQueryUI – The Magic From Behind The Curtain

Watching this presentation was a bit like playing with the jQuery UI demo page. Still, it exposed me to some features that I haven’t seen before. The presenter kept things fun, and I left the session feeling like I’ve learned something.

Conclusion

The Stir Trek organizers, who, as far as I know, are all volunteers, did an amazing job creating an event for 600 people that cost so little and delivered so much value. I haven’t even discussed the freebies like the O’Reilly e-book and the discounts on training, etc. Don’t miss this conference next year!

Tagged , , , , , ,

In LINQ, Don’t Use Count() When You Mean Any()

If you have a list, array, or query in a C#/LINQ application and need to check and see if the list is empty, the correct way to do this is to use the Any() extension method:

if (q.Any())
{

Similarly, you can check to see if any elements in the list meet a certain condition:

if (q.Any(i => i.IsSpecial))
{

If the query provider is something like LINQ to Entities, this will be translated into fairly efficient SQL using EXISTS.

For some reason, I see a lot of people write this code using the Count() extension method instead (maybe they don’t know about Any()?), like this:

if (q.Count() > 0)
{

This is wrong for two reasons:

  • It’s imperative instead of expressive. Using Any() tells the query provider, "Please determine if the list is non-empty using the most efficient way you can." Using Count() > 0 tells the query provider, "Please do exactly what I tell you, regardless of what I really need." Because LINQ is intended to support a variety of query providers, it is important to be expressive instead of imperative, and to let the provider specialize the implementation.
  • Because we don’t actually care about the exact count of items in the list, only that it is greater than zero, using the Count() function can cause the provider to do considerably more work than necessary. Consider a linked list, or a SQL-based provider.
Tagged , ,

A Math Primer for Gentry’s Fully Homomorphic Encryption

A couple of weeks ago, I wrote What Is Homomorphic Encryption, and Why Should I Care? In that post, I promised to share my C# implementation of the algorithm from Craig Gentry’s CACM article. Before I can do that, though, I need to explain some of the math involved.

Perhaps surprisingly, it’s actually very simple. (I say "surprisingly" because much of the math and technical papers on encryption is decidedly not simple, including that of Gentry’s first fully homomorphic scheme, which was based on ideal lattices.)

This scheme created by Marten van Dijk, Craig Gentry, Shai Halevi and Vinod Vaikuntanathan, however, uses basic arithmetic operations like division, with a few uncommon, but easy to understand, variations.

The nature of homomorphic encryption also dictates a programming style which will seem unfamiliar to users of most high-level languages, but very familiar to anyone who has ever taken a digital circuits class.

Integer Division

What is the integer quotient of 10 / 6? Most people would probably say "1, remainder 4." But the exact answer, in the domain of real numbers, is 1.666666 (repeating), which is closer to 2 than the previous answer of 1. So another way we could answer this question is "2, remainder -2." This quotient is closer to the exact answer, and, correspondingly, has a smaller remainder.

So which answer is correct? Arguably, both of them. As long as the following rule holds:

dividend = divisor * quotient + remainder (the division rule)

…then we have a valid answer.

Which method should you use? It depends on why you’re doing the division. If you’re computing how many cupcakes each child can have at a birthday party, then you’d better use the answer with the positive remainder. If you’re computing how many panels of fencing you need to surround your yard, then you’d better use the answer with the negative remainder.

In fact, there are at least five different, valid algorithms for selecting a quotient and remainder for a given integer division problem.

The homomorphic encryption scheme used in the van Dijk, et. al. paper and in Gentry’s CACM article uses "R-division":

  1. Compute the real quotient QR.
  2. Compute the integer quotient QZ by rounding QR to the closest integer.
  3. Compute the remainder R = Dividend - Divisor * QZ

This is probably different than the method you learned in elementary school, but both are valid. Importantly, it’s probably different than what the integer division and remainder operators in your favorite programming language do.

Modular Arithmetic

If you’ve ever seen a 12 hour, analog clock, then you’ve worked with modular arithmetic. The time of 10:00 today is the same position on the clock as 10:00 p.m/22:00. Another way to say this is:

10 ≡ 22 mod 12

This reads: "10 is congruent to 22 modulo 12." In other words, we just ignore the "extra" 12 hours in the measurement of 22:00, because it’s the same clock dial position as 22:00, only with a circle around the dial.

Naturally, we can perform arithmetic operations like addition and subtraction and compare the congruence of the result. I can say "10:00 + 5 hours means that the hour hand will point to 3 on the clock," or:

10 + 5 ≡ 3 mod 12

Modulo 2 Arithmetic and Binary Operations

You can use any number as the modulus. When we make analog clocks, we use 12. A modulus which is particularly interesting in computer programming is 2. Arithmetic operations mod 2 correspond to binary operations, e.g.:

0 + 0 ≡ 0 mod 2
0 + 1 ≡ 1 mod 2
1 + 0 ≡ 1 mod 2
1 + 1 ≡ 0 mod 2

From this example, you can see that addition modulo 2 is the same as the binary operation XOR. It turns out that subtraction is exactly the same thing (for mod 2 only!):

0 - 0 ≡ 0 mod 2
0 - 1 ≡ 1 mod 2
1 - 0 ≡ 1 mod 2
1 - 1 ≡ 0 mod 2

Multiplication modulo 2, on the other hand, corresponds to the binary operation AND:

0 * 0 ≡ 0 mod 2
0 * 1 ≡ 0 mod 2
1 * 0 ≡ 0 mod 2
1 * 1 ≡ 1 mod 2

Mod in Programming

This is all very simple. However, a word of caution is in order here for anyone who has used a high-level programming language. Most languages have an operator like mod (in Delphi) or % (in C#, which is commonly pronounced "mod"). However, this operator is not strictly modular congruence. It is more like a remainder, although, as we’ve seen, different people and different programming languages might choose to compute a remainder in different (but hopefully valid) ways.

Remainders and congruence are often the same. In fact, the congruence relationship and the integer division remainder for the examples above are all the same. For example:

10 ≡ 22            mod 12
22 / 12 = 1 R 10

However, it’s easy to show examples where this is not true:

22 ≡ 34              mod 12       (1)
34 / 12 = 2 R 10                  (2)
-22 ≡ 2              mod 12       (3)
-22 / 12 = -1 R -10               (4)

(1) is probably not the most common choice; 10 would be a more common answer, as with (2). (1) is nevertheless correct as a statement of congruence. Now compare (3) with (4). The most common way to compute a modulus is to pick the smallest positive number. But the most common way to perform integer division is so-called "T-division", where you select the quotient closest to zero and then calculate the remainder, resulting in a negative remainder when either the dividend or the divisor is negative.

Programs as Digital Circuits

A homomorphic encryption scheme, in addition to the usual Encrypt and Decrypt and KeyGen functions, has an Evaluate function which performs operations on the ciphertext, resulting in the ciphertext of the result of the function.

Obviously, this necessitates a different style of programming than that afforded by the typical programming language. In particular, code like this:

bool a = b ? c : d

…(where a, b, c, and d are all bools) is impossible, because the value of b is encrypted, so the program cannot know what to assign to a. If, however, we can perform binary operations on the ciphertext of b, then we can rewrite the statement above as:

bool a = (b & c) | ((!b) & d)

…or its homomorphic encryption equivalent:

CypherText a = (b h_and c) h_or (h_not(b) h_and d)

…where the h_* operators are the homomorphic equivalents of the usual Boolean operations and a, b, c, and d are now of type CypherText.

Note that the version with the ternary operator and the version with the AND and OR operators are not strictly the same, although their results are; the ternary operator is lazy, whereas the AND and OR version uses complete evaluation. This is necessary when the values are encrypted. It also means that the function may be inefficient.

Intuitively, it’s easy to see that any referentially transparent function can be reduced to such operations; this is what compilers and operating systems do under the hood anyway. I’m not going to go into any detail on how this is done; get an introduction to digital circuits book if you are interested in the particulars.

Gentry’s article notes that other changes in programming style are necessary when performing operations within a homomorphic encryption scheme. For example, the size of the output of a function must be set in advance. Even if the plaintext is variable-length, the ciphertext must be of a known length, which corresponds to the number of "wires" in the circuit. The plaintext, therefore, would have "whitespace" at the end or be truncated, depending upon its size.

Minimizing Boolean Functions

In my first post on homomorphic encryption, I mentioned that Gentry’s encryption schemes can be considered fully homomorphic because they support two homomorphic operations, corresponding to Boolean AND and XOR. I’d like to go into a little bit more detail as to why that is true.

Many combinations of Boolean operations are equivalent. Perhaps the most famous are De Morgan’s laws. There are a variety of techniques for converting one function using Boolean operations into an equivalent function with different operations. This is often done in order to produce a simpler or more efficient function, but can also be done in order to use a specific type of Boolean gate.

It is possible to use combinations of NAND or NOR gates, for example, to produce circuits which perform a Boolean AND, OR, NOT, etc. Hence, a cryptosystem which supported a homomorphic NAND operation could be considered "fully homomorphic."

Similarly, having both AND and XOR gates available allows you to produce the same outputs as all other Boolean gates, as, for example, NOT p is the same as p XOR 1 and we can see by De Morgan’s laws that with NOT and AND we can implement OR.

Therefore, we can see that a cryptosystem can be considered fully homomorphic if it supports enough homomorphic operations to perform any logical Boolean operation. In particular, a cryptosystem which supports any of the following homomorphic operations would qualify:

  • Just NAND
  • Just NOR
  • Both XOR and AND

What’s Next

If you understand the math above, you should now be able to follow along as I implement Gentry’s algorithm for "somewhat homomorphic" encryption. As you’ll see, the "somewhat homomorphic" encryption will be the first step towards a fully homomorphic cryptosystem.

Tagged , , ,

Want to Beta Test the Next Version of InterBase?

The InterBase roadmap says that the next version will probably include native 64 bit and Windows 7 support, cloud deployment, and enhance password security. Want to help beta test it? You can sign up now.

Tagged

Bad Behavior has blocked 713 access attempts in the last 7 days.

Close