0

Originally, I was following along in this ASP.NET MVC4 tutorial on using EF: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

Everything worked fine there, so I tried to replicate it in WPF and I can't get the localDb (MDF) file to generate.

I've tried this tutorial on EF which uses a Console app instead of ASP.NET: http://msdn.microsoft.com/en-us/data/jj193542

Again, it works fine in the Console, but trying to do it in WPF in the MainWindows.xaml.cs fails.

Any ideas?

EDIT: (I'm using VS2012 and .NET 4.5)

EDIT2: Yes, I'm getting this error:

{"'The invocation of the constructor on type 'MyProgram.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'."}

EDIT3: Apparently that exception above is very common and unhelpful. The solution I found elsewhere is to press CTRL + ALT + E and check every box and click OK and re run the application. Then a helpful exception shows up. (I'm still figuring out what this new one is.)

EDIT4: Well, I got it working. A call to Console.ReadLine() is illegal in WPF. Who knew? (I guess it makes sense.) Now I'm trying my own CodeFirst design and running into a different error.

Pretzel
  • 8,141
  • 16
  • 59
  • 84

3 Answers3

1

Check your user folder (C:\Users\UserName)

When I followed the example, the mdf file got created here.

Jimmie R. Houts
  • 7,728
  • 2
  • 31
  • 38
  • Are you getting any errors when you run it or is the file just not being generated? – Jimmie R. Houts Sep 17 '13 at 21:16
  • According to http://stackoverflow.com/questions/10540438/what-is-the-connection-string-for-localdb-for-version-11 you need to have .net 4.0.2 or higher, if you are using VS 2010 there is a patch referenced in the SO answer as well, not sure how relevant this is since it is working on other project types... – Jimmie R. Houts Sep 17 '13 at 22:08
  • What does your MainWindow.xaml.cs look like? – Jimmie R. Houts Sep 18 '13 at 00:12
1

So the answer was to turn on all exceptions by pressing CTRL + ALT + E and selecting all check boxes.

Then I got the real exception which was: Cannot open database "MyDatabase" requested by the login. The login failed.

The solution was found here: EF 4.1 code first causes weird (login) runtime errors

I had to delete the SUO file (and re-enable everything again via CTRL + ALT + E) and then I found another (new) error, which I fixed, and everything was fine.

Community
  • 1
  • 1
Pretzel
  • 8,141
  • 16
  • 59
  • 84
0

Create (don't copy) the EF context in a separate project. Then it can be accessed from a console application or a WPF application. That separates the concerns and will lead you to diagnosing the problems quicker. Note that you will need to add the connection string as found in the EF project appconfig in any project which is a consumer of the EF project you created.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122