Here is how to make and use an .edmx assuming you have a database like this. To start with why don't we make an MVC 4 Web API Web Application? Next, let's add a new ADO.NET Entity Data Model called (MyModel.edmx) to the Models folder like so:
We will see a dialog box like so. Let's push forward with the "Generate from database" option.
We will get a dialog box like this next:
It won't be prepped as shown above however. (Note that "RepoEntities" will end up as the name of our data context based on the "Repo" database.) We have to prep it ourselves by clicking on "New Connection..." which will bring up yet another dialog box which looks like this where we have to fill in how to have at our database:
In the last bit of the Entity Data Model Wizard you will check checkboxes for whether or not you want to suck tables, views, stored procedures, or some mixture of the three into the .edmx. The Web.config file will be doctored up to include a connection string on the other side of the wizard. Our .edmx:
I right-clicked in the middle of the .edmx and picked "Add Code Generation Item..." from the menu which popped up. This took me to different dialog box where I had the option to generation an "EF 5.x DbContext Generator" called "Model1.tt" which I did add. I then saved my .edmx. I have a few more files now. If ever some of these files get messed up one may right click on "Model1.tt" and pick "Run Custom Tool" to remake them. Moving forward, I put the following silly code in the HomeController just to test:
using System;
using System.Web.Mvc;
using OldSchool.Models;
namespace OldSchool.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
using (RepoEntities myStuff = new RepoEntities())
{
Guid myGuid = Guid.NewGuid();
foreach (Yang yang in myStuff.Yangs)
{
string aloha = "hi/bye";
}
}
return View();
}
}
}
To keep Yangs from being an ambiguous reference which was confused as to whether the compiler was to read from my new .tt file and/or something that was there before it, I had to destroy some of the old files in the Models folder at the Solution Explorer as seen on the other side of my clean up immediately ABOVE. I was able to run the app, hit a break point and inspect Yangs as seen BELOW. Also on the other side of the hitting the breakpoint a few times, the application does bring up the home page without error. I get this when I move my mouse over "yang" upon first hitting a breakpoint:
Addendum 7/13/2013: Try to ignore Guid myGuid = Guid.NewGuid(); in the code here. It is accidental.
No comments:
Post a Comment