I am new to asp.net mvc. I would like to use EF code first approach.I need 3 different database in my project. What I understood from internet till yet
1: That means need to create 3 diff connectionstring in my config file.Appropriate 3 diff context class will be there (project).If context class and connectionstring name is different,need to mention in it's constructor with the help of base keyword. Each context class have proprties as all table of that database.When I build the project, context class will create database with name mention in connectionstring along with all dbcontext property in that database as table if not already exist.
2: If I want to add new table in my database,here I am using EF code first approach So I need to create a class for that and mention it in it's appropriate dbcontext class only and build project. Enable migration Add Migration Update database
This will add table to my database and migration file in project which has up and down method.
Is this a right approach to do.I want to update only perticular databse not all. Shall I write name of database while migrating
Rijwan AnsariPosted Jul 12, 2021, 6:47 AM
Hi,
If you are using 3 or more databases then better to seperate context class as well. It will be easy to manage and use separate connection sting for each contextDB. Also it will be easy for developers to know which database they are working on based on.
Regarding database migrations or update you can pass context name in command as shown.
Regards
Rijwan
Sachin SinghPosted Jul 12, 2021, 7:34 AM
Piyush PansuriyaPosted Jul 12, 2021, 6:54 AM
SYNTAX Update-Database [-SourceMigration] [-TargetMigration ] [-Script] [-Force]
[-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ]
[-ConnectionStringName ] [-AppDomainBaseDirectory ] []
Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force]
[-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ]
-ConnectionString -ConnectionProviderName [-AppDomainBaseDirectory ]
[]