I am following "Developing-Step-By-Step-Angular" to add my own application. I want to keep all built-in functions in "default" database and store my own application data in a separated database.
I added another ConnectionString in appsettings.json, like { "ConnectionStrings":{"Default": "//default string", "AppData":"//application string"}, ......}
I wonder what is the best practice to use the separated connection "AppData". For example in "Creating Person Entity" step, how do I reference the connectionString to create [Table("PbPersons")] into "AppData" database?
Actually, the "AppData" database and tables exist already. I don't need to migrate/seed. I just need to map the schema from existing table to Person entity. Please advise!
Thanks,
14 Answer(s)
-
0
Hi,
You can crate a second DbContext for your AppData and use new connection string for second DbContext. You can check this example <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/MultipleDbContextDemo">https://github.com/aspnetboilerplate/as ... ontextDemo</a>.
If your DB already exists, after setting connection string, add migration and remove it's content for using it as a starting point, then you can use migrations for changes in your second dbContext.
If you don't want to use migrations for second db at all, you can disable it.
Thanks.
-
0
Can I clone/download your example from git?
Thanks,
-
0
Hi @fguo,
Yes, of course. This is an open source project.
Thanks.
-
0
I cloned it to my local machine and studied it. It seems for “Boilerplate” users, instead of Prime users. To implement the same concept into ASP.NET Zero project, I tried the following steps, but it failed :( . Let me describe it step-by-step as following, and hope you can tell me what I missed.
- In .Web.Host project, appsettings.json, add another ConnectionString: “App1” with same value of “Default”.
- In .Core project, Costs.cs, add a const: public const string ConnectionStringName1 = "App1";
- In .EntityFrameworkCore project, create a new folder: App1EntityFrameworkCore.
- Copy all files in EntityFrameworkCore folder into App1EntityFrameworkCore folder, and replace the file name prefix with “App1”, so new files named as App1xxx.cs.
- Set new namespace ProjecrtName.App1EntityFrameworkCore in all new copied files.
- Change all “DbContext” words to “App1DbContext” in all new copied files.
- Change ConnectionStringName to ConnectionStringName1 in App1DbContextFactory.cs.
- Build and run successfully without compile error.
Then, I tried to connect an existing table “Customers” in another database with “App1” connectionString:
In .Core project, I added a file: App1\Customer\Customer.cs. It contains a class [Table("Customers")] public class Customer: FullAuditedEntity { [Required] [MaxLength(200)] public virtual string SCM_Name { get; set; } // It is an existing column name. }
In App1DbContext.cs file, I cleaned old code and added new code as below: public class App1DbContext : AbpZeroDbContext<Tenant, Role, User, App1DbContext> { public virtual DbSet<Customer> Customers { get; set; } public App1DbContext(DbContextOptions<App1DbContext> options) : base(options) { } }
I tried to add migration to see if this new table can be created (I am not going to update database). I run Add-Migration "Added_Customers_Table", but it prompts me “More than one DbContext was found.” So I run as Add-Migration "Added_Customers_Table" -Context "App1DbContext". It generated a class Added_Customers_Table, but it does not contain the creation of new table. Instead, it contains many tables, just same as “Initial_Migration” class. :?: :?: I think something wrong from this point.
I continue to add a service “GetCustomers” and it successfully added on Swagger as /api/services/app/CustomerService/GetCustomers. But, the Unit test failed, and if I directly run localhost:22742/api/services/app/CustomerService/GetCustomers, I got “internal error”: {"result":null,"targetUrl":null,"success":false,"error":{"code":0,"message":"An internal error occurred during your request!","details":null,"validationErrors":null},"unAuthorizedRequest":false,"__abp":true}
I lost here. Please advise how to implement “Multiple Database” in ASP.NET Zero project.
Thanks,
-
0
Hi,
Can you try to add migration using configuration for your second dbContext ?
Add-Migration MYMIGRATION -ConfigurationTypeName ConfigurationB
Also be sure that both dbContexts have different configuration classes.
Thanks.
-
0
Do you mean to use a parameter "-ConfigurationTypeName"? I used a command as:
Add-Migration "Added_Customers_Table" -Context "App1DbContext" -ConfigurationTypeName "App1DbContextConfigurer"
That is wrong. "A parameter cannot be found that matches parameter name 'ConfigurationTypeName'".
The above "App1DbContextConfigurer" is the class name for the new DbContext:
public static class App1DbContextConfigurer { public static void Configure(DbContextOptionsBuilder<App1DbContext> builder, string connectionString) { builder.UserSqlServer(connectionString); } }
Can you give me the exact Add-Migration command?
-
0
Hi,
I was trying to suggest this:
Add-Migration "Added_Customers_Table" -ConfigurationTypeName "App1DbContextConfigurer"
If that does not work, please send your project to us and we will check it. You can send it to <a href="mailto:[email protected]">[email protected]</a>.
Thanks.
-
0
I tried again by copy/paste your command in VS 2007 Package Manager Console, and got the error massage below:
PM> Add-Migration "Added_Customers_Table" -ConfigurationTypeName "App1DbContextConfigurer" Add-Migration : A parameter cannot be found that matches parameter name 'ConfigurationTypeName'. At line:1 char:39
- Add-Migration "Added_Customers_Table" -ConfigurationTypeName "App1DbC ...
-
~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidArgument: (:) [Add-Migration], ParameterBindingException
- FullyQualifiedErrorId : NamedParameterNotFound,Add-Migration
What did I miss?
By the way, how do I send you my project? Shall I zip all files in aspnet-core\src*.EntityFrameworkCore, and attach this zip file on email?
Thanks,
-
0
Hi,
You can zip it and send it via google drive. You need to send all of your project not just EntityFramework.
Thanks.
-
0
I have emailed a link to <a href="mailto:[email protected]">[email protected]</a>.
Thank you!
-
0
Hi @fguo,
I have got your project but couldn't check it yet. We were very busy with v4.1 of AspNet Zero. I will get back to you in a short time.
Thanks.
-
0
Hi @fguo,
I'm sharing your message in case anyone else faces same problem.
I have found the cause, which is the missing interface for the 2nd DB entities.
Thanks.
-
0
I don't understand. Where does your quote come from?? What is the "interface for the 2nd DB entities"?
-
0
Hi @fguo,
Sorry, It was another problem and I thought it was you but I was wrong. The problem was similar to your case.
Becase of that I didn't check your problem but I will take a look at it today.
Thanks.