7

VS2010 Pro + SqlServer Express.

Having been dropped into ASP.NET MVC 3 with no guidance but the web (2 books on order), I can't even get off the ground.

The MVC itself I get. Not a problem. PHP, Ruby, and even ghastly WebForms firmly tucked into my toolbelt, with a long history of C++ QT client-server development before that.

Tying ASP.NET MVC 3 to a database using EF4 ORM is killing me.
The goals:

  1. Use database modeled by DBA. I can specify all naming conventions, but code first is not an option!
  2. Import to EDMX. This will be regularly updated using VS tools from the DBA's DB, never edited directly.
  3. Generate partial classes from EDMX, for use as model. This will regularly be updated using VS tools, never edited directly.
  4. Use 'buddy' to extend above model class with code as the Controllers/Views need.
  5. Intuitively use the resulting model, pass it to the view, retrieve posts into it for insert/save, etc...

I've seen and read so many blogs, forum posts, walkthroughs, and stack overflow posts regarding this very use case. I even tried riding the magic unicorn, followed by the latest 4.2beta1 with DbContext generators.

But can't get off the ground. I follow instructions, but just not understanding how to do anything with it. What conventions does the 'buddy' require (if any)? How do I use it? How do I get data with it? How do I write data? Every example looks different. MVC guides are always focused on the UI side. EF guides don't cover usage in the MVC.

These are basic questions, and I'm feeling like the most incompetent idiot in the WWW right now.

Is anyone out there currently using MVC3 & EF4.x in the way I describe above?

Community
  • 1
  • 1
user912639
  • 71
  • 1
  • 2
  • Is your question how do you build MVC3 EF4 with an existing Database? – Doug Chamberlain Aug 25 '11 at 18:17
  • Doug: Yes, exactly. Stuck with EF for ORM. Only authorized to use MS released and supported technologies, no third parties (though if I can give **concrete** justification, with cited examples, might be able to get exception). – user912639 Aug 25 '11 at 18:47
  • I got it to work very easily. I can send you all my code. – Doug Chamberlain Aug 25 '11 at 18:58
  • "_... and I'm feeling like the most incompetent idiot in the WWW right now._" - My [insert family-member here] wouldn't like h[is|er] well-earned title taken away from h[im|er]. – ckittel Aug 25 '11 at 19:03
  • Doug: Thanks, I'd appreciate any working db-first examples. How should I get it? – user912639 Aug 25 '11 at 19:21

3 Answers3

2

This video is a good starting resource. Its a video of a guy creating an app from scratch that uses entity and a sql database (though he makes the db in the video, its still good for seeing some basics in action). You can see how he pulls data from the database, displays it on the page, and saves changes back to the database.

Kyeotic
  • 19,697
  • 10
  • 71
  • 128
0

The first question I would ask is why are you stuck on using EF as an ORM or even insisting an ORM at all? I'd choose tools to suit the job here, especially given the constraints of the data layer.

Buddy classes were a concept invented in a day when the main .NET ORMs had no code-first option as ORM-encumbered class instances really don't behave well under things like model binding. Nevermind you could not decorate them with the DataAnnotations one used to indicate fields were required. Typically, the technical requirement is to use [MetadataType] attributes to tie your buddies to your models and perhaps something like AutoMapper to map data to and fro.

All that said, as a guy who has a few apps with lots of buddies and lots of automapping going on, you might want to think otherwise -- it is a bit of a maintenance nightmare. I'm living it.

Wyatt Barnett
  • 15,573
  • 3
  • 34
  • 53
  • 1
    Yes, stuck with EF. Only can use MS released tech. 'Buddy' is not a specific requirement, but the only way I've found to fulfill the no-code-first mandate, and let ORM mapping be handled automatically. Looked at Automapper, but it seems like it just adds another layer of abstraction between the two parts I can't get working right now! Something else to configure; do not want. Just want to import my model, and annotate it as the MVC expects, then update it over time. EF is supposed to do that (with partial classes), but I can't figure out how to implement. – user912639 Aug 25 '11 at 18:54
0

There are some really good getting-started videos and tutorials right on ASP.NET MVC's site. The "Model (Data)" section is Entity Framework focused and touches on hot/trending topics like Repositories and Units Of Work.

ckittel
  • 6,478
  • 3
  • 41
  • 71
  • 1
    Thanks, watched most of them. Not interested in hot topics; just a solid working way to do database-first in as automatic a way as possible. Having a hard time stringing together different examples for the unrelated technologies used in MVC to get something that works within our needs. – user912639 Aug 25 '11 at 18:48
  • Understood. For what it's worth, all new interns at our company go through these videos and tutorials. It seems they are mostly walking away with a good understanding of the MVC + DB-First EF system. Of course there is nothing like experience to really learn something; but these give everyone the headstart they need. – ckittel Aug 25 '11 at 18:53