MarimerLLC / cslaforum

Discussion forum for CSLA .NET
https://cslanet.com
Other
31 stars 6 forks source link

Blazor app and 'local' DataPortal #924

Open Chicagoan2016 opened 4 years ago

Chicagoan2016 commented 4 years ago

Question This may be a very simple question : ) but how do we connect to a on-premise sql instance? I am used to using connection strings from configuration files. We are also going to use the default 'local' data portal so no application server. Kind Regards

Version and Platform CSLA version: 5.xx OS: Windows Platform: Blazor

BlagoCuljak commented 4 years ago

I'm not sure what's the difference between local Dataportal in the one in the cloud. Both of them connect to SQL DB (local or cloud) using some config files.

If you are using EntityFramework for ORM and want to connect to your DB using appsettings.json then your need to change:

  1. In EF Dbcontext

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            IConfigurationRoot configuration = new ConfigurationBuilder()
            .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
            .AddJsonFile("appsettings.json")
            .Build();
            optionsBuilder.UseSqlServer(configuration.GetConnectionString("connstring"));
        }
  2. In Dataportal, Startup.cs in "public void ConfigureServices(IServiceCollection services)"

    services.AddDbContext<Dbcontext>(options =>
       options.UseSqlServer(Configuration.GetConnectionString("connstring"), 
       sqlServerOptions => sqlServerOptions.CommandTimeout(600)));
  3. Add appsettings.json, add the connection:

    {
    "ConnectionStrings": {
    "connstring": "Server=192.168.1.1;Initial Catalog=DataBaseName;Persist Security Info=False;User ID=sa;Password=ChangeMyPassword;MultipleActiveResultSets=False;"
    }
Chicagoan2016 commented 4 years ago

@BlagoCuljak thanks

rockfordlhotka commented 4 years ago

Client-side Blazor does not have any way to talk directly to a database. afaik System.Data, entity framework, etc. are not available in the browser.