Base solution for your next web application
Open Closed

Why ABP uses Code First, why not DB First? #4758


User avatar
0
manojreddy created

Could you please explain this with some practical examples? What are the advantages of Code First over DB First?


6 Answer(s)
  • User Avatar
    0
    hitaspdotnet created

    In code first approach we will first create entity classes with properties defined in it. ABP Entity framework will create the database and tables based on the entity classes defined. So database is generated from the code.

    Advantages 1.You can create the database and tables from your business objects. 2.You can specify which related collections are to be eager loaded, or not be serialized at all. 3.You can use amazing Microsoft Entity Framework features in your entities 4.Database version control. 5.Good for small applications

    Database First Approach Disadvantages

    1.Using an existing database to generate a .edmx model file and the associated code models results in a giant pile of auto generated code. 2.When you need to add any functionality to generated model you have to extend the model class generated.

    In the ABP framework everything is clean and easy so you can migrate from DB first to Code first easily. Use amazing ABP Zero RAD tools for generating your entities and enjoy.

  • User Avatar
    0
    manojreddy created

    What about others opinion?

  • User Avatar
    0
    strix20 created

    Db first does not exist any more. It was not included in EF core.

    There is a huge misunderstanding in the community, in the assumption that Code First refers to how the database is created. This is not the case at all. Code First refers to EF configuration using attributes, pattern matching, and the fluent API, as opposed to Database First which used an XML based (edmx) configuration for EF.

    There is no reason you cannot use a SQL first approach. In fact, that is what we do in our project, using the alpha3 version of DbUp for .net core (we have a small team, and lack a DBA, so database operations are managed by our developers, and it's easier to have scripts included in our deployment process than to keep them isolated.)

    If you want to take a SQL first approach, nothing really changes, except that you cease to create new migrations using the add-migration tool. You write the entity classes, and you configure the relationships in OnModelCreating. Assuming the database has had the proper SQL scripts run to generated the tables and procedures you are mapping to, then your code will work just fine.

  • User Avatar
    0
    strix20 created

    I would add that in EF6, you could generate code models from a database using the code first approach.

    Code scaffolding from database was a stretch feature for EF core 2.1, but it did not make the cut for release, it looks like. You can track the feature here: [https://github.com/aspnet/EntityFrameworkCore/issues/831])

  • User Avatar
    0
    faisalalam created

    That'sa good option. I bought this and customizing generated code for asp.net zero.

    <a class="postlink" href="https://www.junnark.com/Products/AspCoreGen2MVC/">https://www.junnark.com/Products/AspCoreGen2MVC/</a>

    I am waiting for aspnet zero RAD tool enhancement with the foreign key.

  • User Avatar
    0
    alper created
    Support Team

    ABP is an opinionated framework and the right way is being decided by the tech trends. ABP implements domain driven design. In that approach code-first is the way to go. If you start with the database, you practice Data Driven Development. In DDD, code comes way before thoughts about database design. And even Microsoft Entity Framework Core does not implement DB first approach too.