skip to Main Content

I am using EF and in MY web config I have removed the Connection string after adding the model for the first time. So it looks like this now:

<connectionStrings> </connectionStrings>

I have modified my Model.context.cs file where I have added one parameter in the Constructor of Entities. Below is the code:

public QMatchServerEntities(string connectionString)
       : base(connectionString)
    {

    }

When I make connection and create object of entity I send the whole connection string in the parameter. I used this case because it was a requirment from client side that DB password should be encrypted in the code.

Model.QMatchServerEntities qMatch = new Model.QMatchServerEntities("Connection string")

What I wanted to know is that, after this when I update the model it ask me to create new connection first because EF is unable to find the connection string of this model in web config. Is there any way to fix that?

Thank you.

2

Answers


  1. You can implement the migration of the database during the startup of you web app.
    I presume you using .NET 6.

    First, create a new class named DbInitializer.cs, in your project like this :

    using Microsoft.EntityFrameworkCore;
    
    namespace YouAppNamespace.Data
    {
        public static class DbInitializer
        {
            public static void Initialize(ApplicationDbContext ctx)
            {
                // used this for ensure database is created and for appy all migrations at runtime
                ctx.Database.Migrate();
            }
        }
    }
    

    After that, in you Program.cs file add the code mentionned under "Add this part" :

    app.UseAuthentication();
    app.UseAuthorization();
    
    // Add this part
    var serviceScope = app.Services.GetRequiredService<IServiceScopeFactory>().CreateScope();
    var ctx = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
    DbInitializer.Initialize(ctx);
    ctx.Dispose();
    serviceScope.Dispose();
    
    app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
    app.MapRazorPages();
    
    app.Run();
    

    Don’t forget to add the dependencies.
    Tell me if it works or not.

    Login or Signup to reply.
  2. You need to instruct the DBContext to use that provide connection string, so your code should override the OnConfiguring(…) method somewhat like this:

    public class ApplicationDbContext : DbContext
    {
       private readonly string _connectionString;
       public ApplicationDbContext(string connectionString)
       {
         _connectionString = connectionString;
       }
    
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
        optionsBuilder.UseSqlServer(_connectionString);
      }
    }
    

    You can find more information over here: https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search