Tech Off Thread

7 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Is Code First with Existing Database no longer available?

Back to Forum: Tech Off
  • User profile image
    RodAtWork

    I've started a new MVC 5 project. It's been a while since I've started one and I think I may have gotten something wrong. Anyway, I want to use EF 6.1.*. I created a class to represent a table and then added a controller using the class I created. I had it use EF and create the views and CRUD operations within the EF generated controller.

    However, things aren't working well. Skipping over some stuff I'm wondering if I made a mistake by not including EF using Code First with Existing Database. (Like I said, its been a little while and where I now work they always create a EDMX file instead. I'm trying to use Code First in this app.) So I checked out the video Code First to Existing Database (EF6.1 Onwards) here on Channel 9. I tried to follow it now, but couldn't find where to now add EF, after the fact, in VS 2015.

    Have I made some serious mistakes? Would it be better if I just scrap everything I've done and start over? Or can I fix the problem as I have it in my current project?

  • User profile image
    figuerres

    Sounds like you just need to take one step back and make it all work.

    I am doing WebAPI and OData 4  with EF 6.

     

    I have two VS projects: one is the web app that builds my controllers for the rest api's

    the other is my EF model that is compiled as a dll for the web app to use.

    for a clean start I create a vs class library project, add the nugget for EF 6.*

    then you can use the VS add dialog to add an EF data model, point that to the data base and it will scaffold all the classes it needs.

    no more edmx file.

    the results are a nice set of  POCO c# classes

  • User profile image
    cheong

    I didn't follow EF since 5 because my latest 2 companies doesn't use it, but they've dropped EDMX files? Did they dropped the .TT files as well? The code I written in the past did take advantage of the T4 template to add custom attributes and generate a lot of helper classes for the BLL.

    Recent Achievement unlocked: Code Avenger Tier 4/6: You see dead program. A lot!
    Last modified
  • User profile image
    figuerres

    , cheong wrote

    I didn't follow EF since 5 because my latest 2 companies doesn't use it, but they've dropped EDMX files? Did they dropped the .TT files as well? The code I written in the past did take advantage of the T4 template to add custom attributes and generate a lot of helper classes for the BLL.

    I think the tt files are still used but yes no more .edmx file.

    the wizard asks what tables / views to map and generates a bunch of code files are very clean and simple.

    if I am just adding a column to a table I often just edit the code and not re-run the wizard.

    also the mapping of table relations is shown in code files that you can edit as needed. much more of just compiling the code and no big lag waiting for the edmx file to be read and parsed and all that.

     

  • User profile image
    spivonious

    I find it interesting that most EF models I've seen are carbon copies of the database structure. Isn't the whole point of an O/RM to map the domain model of the application to the data model in the database? If everything is 1:1, just use a DataSet and get it done a lot faster.

  • User profile image
    figuerres

    I find it interesting that most EF models I've seen are carbon copies of the database structure. Isn't the whole point of an O/RM to map the domain model of the application to the data model in the database? If everything is 1:1, just use a DataSet and get it done a lot faster.

    I think that you are missing a few details, for example many to many link tables are not shown in an EF model as an actual table, what they do works right but the table is not shown.

    also while the table model is just that there are a number of things you can do if you need to.

    for example I have an interface that I use that defines a set of base columns used in many tables that in turn helps me with some generics code in my OData 4 controllers.

    you can change the model in other ways if you need to. the generated code is a starting point and it's up to the developer to determine the need to take it farther.

    I kind of like how it is now I can tweak it but it does not add in stuff that I do not need. I can add based on the need.

  • User profile image
    spivonious

    @figuerres: Yes, there are some benefits to EF over Datasets, but IMHO the real advantage of an ORM is to remove the database structure from the equation. Map multiple classes to the same table, map multiple tables to one class, etc. Make your domain model work best for your application; don't just copy the database structure if it isn't ideal.

    I think that too many people reach to an ORM these days before considering other options. A lot of the time, a simple DataReader (or better yet, Dapper) is the best solution. When I see people using EF for read-only data displayed on a web page, I die a little inside.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.