locked
Entity Framework Approach RRS feed

  • Question

  • User-797751191 posted

    Hi

      I have already tables in Sql . Which Approach should be used with Entity Framework.

    Thanks

    Thursday, October 17, 2019 7:30 AM

All replies

  • User753101303 posted

    Hi,

    You have also "Code First from Database" : https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/workflows/existing-database which generate the code you should write to generate this database.

    Thursday, October 17, 2019 7:52 AM
  • User-821857111 posted

    As PatriceSC said, scaffold the model classes from your existing database: https://www.learnentityframeworkcore.com/walkthroughs/existing-database. Once you have done that, you need to keep your model classes and the db schema in sync with each other. The recommended approach is to make changes to the model classes and then use migrations to update the database: https://www.learnentityframeworkcore.com/migrations. However, if you prefer, you can make changes to the database and then re-scaffold the model class files by adding -force to the original command. Any changes that you make to the model classes e.g. adding Data Annotations to them, will be lost if you take this approach.

    Thursday, October 17, 2019 8:54 AM
  • User1120430333 posted

    Any changes that you make to the model classes e.g. adding Data Annotations to them, will be lost if you take this approach.

    So why can't one create the model in another location not losing things in the existing model so that they can do a copy/paste operation of code from the new model to the existing model to not lose existing model code?

    Thursday, October 17, 2019 11:58 AM
  • User753101303 posted

    Hi,

    Or depending on which kind of changes are done to the generated classes you could also use partial classes and/or https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.metadatatypeattribute?view=netframework-4.8 maybe (I see it is now even added back to ASP.NET Core 3).

    Thursday, October 17, 2019 12:12 PM
  • User-821857111 posted

    Any changes that you make to the model classes e.g. adding Data Annotations to them, will be lost if you take this approach.

    So why can't one create the model in another location not losing things in the existing model so that they can do a copy/paste operation of code from the new model to the existing model to not lose existing model code?

    There's nothing to stop you doing that apart from eventually going insane.

    Thursday, October 17, 2019 8:18 PM
  • User409696431 posted

    PatriceSc's post links to the solution I use.  Simply keep the Data Annotations in a separate file, and they won't be overwritten when you make changes to the models.

    Thursday, October 17, 2019 8:51 PM
  • User288213138 posted

    Hi jsshivalik,

      I have already tables in Sql . Which Approach should be used with Entity Framework.

    If you already have a database, I recommend you to use the Database-First Approach.

    In the database-first development approach, you generate the context and entities for the existing database using EDM wizard integrated in Visual Studio or executing EF commands.

    EF 6 supports the database-first approach extensively. Visit EF 6 DB-First section to learn about the database-first approach using EF 6. More information you can refer to this link:https://docs.microsoft.com/en-us/ef/ef6/modeling/designer/workflows/database-first

    Best regards,

    Sam

    Friday, October 18, 2019 2:15 AM
  • User-821857111 posted

    If you already have a database, I recommend you to use the Database-First Approach.
    I thought Microsoft's official advice was to NOT use database first, which is why it has not been excluded in EF Core?

    Friday, October 18, 2019 6:37 AM
  • User1120430333 posted

    DA924

    Any changes that you make to the model classes e.g. adding Data Annotations to them, will be lost if you take this approach.

    So why can't one create the model in another location not losing things in the existing model so that they can do a copy/paste operation of code from the new model to the existing model to not lose existing model code?

    There's nothing to stop you doing that apart from eventually going insane.

    I kind of doubt that. I have done it. I have also just gone into the model and made changes manually too without doing a model regeneration. One shoe doesn't fit all situations.

    Friday, October 18, 2019 10:50 AM
  • User1120430333 posted

    samwu

    If you already have a database, I recommend you to use the Database-First Approach.

    I thought Microsoft's official advice was to NOT use database first, which is why it has not been excluded in EF Core?

    Where is the documentation from MS that states this of not using EF DB First prior to EF Core?

    Friday, October 18, 2019 10:56 AM
  • User409696431 posted

    There is no reason to not use Database-First.  Frankly, I never do anything other than Database-First.  I am well versed in database design, and I want that control.

    I haven't used Core, but you can use Database first there, too, although it's a process: https://www.entityframeworktutorial.net/efcore/create-model-for-existing-database-in-ef-core.aspx

    Friday, October 18, 2019 4:08 PM
  • User1120430333 posted

    KathyW

    There is no reason to not use Database-First.  Frankly, I never do anything other than Database-First.  I am well versed in database design, and I want that control.

    I haven't used Core, but you can use Database first there, too, although it's a process: https://www.entityframeworktutorial.net/efcore/create-model-for-existing-database-in-ef-core.aspx

    Some call it DB first in EF Core, but it really is more like Code first against an existing DB due to the scaffolding that is being used along with there is no more EDMX created.

    Friday, October 18, 2019 5:30 PM
  • User409696431 posted

    "Some call it DB first in EF Core, but it really is more like Code first against an existing DB"

    If the database exists, it's database first, whatever term you want to use.  The point is that the code reflects the database you've already designed and created. 

    Friday, October 18, 2019 5:45 PM
  • User1120430333 posted

    "Some call it DB first in EF Core, but it really is more like Code first against an existing DB"

    If the database exists, it's database first, whatever term you want to use.  The point is that the code reflects the database you've already designed and created. 

    You do know that in EF6 you can do Code first against an existing DB, right? The model is created and there is no EDMX. It's the same thing happening in Core. Core doesn't even have a graphic model designer, like EF6 and previous version have. 

    Friday, October 18, 2019 8:52 PM