Mixing "pure" CodeFirst with CodeFirst From Database?

preno 60 Reputation points
2023-05-13T08:13:57.8433333+00:00

Hi, I have to develope a web application with ASP.NET MVC 5 and EF6 an SqlServer for database.

I have a legacy SQLServer database (let's call it DBStart) on which I have SELECT only access to some tables including Employees data and their Role. My application must read data from DBStart.

I think I have two choices :

a) create two separate DBContext : one for the legacy database and one for the new one (let's call it AppDB). I've tried this solution and it works well.

But I think it could be possible to follow also option b:

b) create the new AppDB and add a LinkedServer to the DBStart legacy database. Create views on the AppDB to join remote tables I need to read.

Now the question is (I'm new with ASPNET MVC and EF6) : which is the correct way to proceed for option b ?

Create first class for views with CodeFirst From Database and then add model classes with migrations?

Which option would you use if you were me : option a or b?

Thanks.

Developer technologies .NET Other
Developer technologies ASP.NET Other
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2023-05-16T05:57:51.92+00:00

    @preno, thanks for the feedback, I recommend that you use two dbcontext, which is suitable for your current problem.

    I suggest that you use ef core to generate the related dbcontext, which means that you have to use asp.net core mvc app.

    When you used ef core, you could use code first for your AppDB database and use database first for your DBStart database.

    Code first article: https://learn.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli

    Database first article: https://learn.microsoft.com/en-us/ef/core/managing-schemas/scaffolding/?tabs=dotnet-core-cli

    The advantage of using ef core is that we don't need to delete the dbcontext manually when we use the database first command or the code first related command again, and it will update the existing database and entity classes.

    BTW, I did not recommend that you create a table in the new db with data read from the remote database. Because if the remote database has the update, you need to update the new db manually.

    Hope my advice could help you.

    Best Regards,

    Jack

    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.