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.

2
  • 2
    Yep. You should probably debug it. Commented Sep 17, 2013 at 20:36
  • Yeah, I think this might be the problem. I just don't know enough about EF yet. Commented Sep 17, 2013 at 23:29

3 Answers 3

1

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

Are you getting any errors when you run it or is the file just not being generated?
According to stackoverflow.com/questions/10540438/… 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...
What does your MainWindow.xaml.cs look like?
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.