Click here to Skip to main content
15,904,652 members

Comments by dolce_sweet (Top 2 by date)

dolce_sweet 18-Sep-23 4:58am View    
I can understand your concerns.

The connection string is stored in appsettings.json.
"ConnectionStrings": {
"Default": "Data Source=192.168.99.101\\SQLEXPRESS;Initial Catalog=DemoDatabase;Integrated Security=True;TrustServerCertificate=True;Encrypt=False"
},
First once without user and password. But I have already tried that too.
My sample tests:
- "Data Source=.\\SQLEXPRESS; Initial Catalog=DemoDatabase; User Id=SimpleUser; Password=SimpleUserPassword; TrustServerCertificate=True; Encrypt=False"
- "Data Source=.\\SQLEXPRESS; Initial Catalog=DemoDatabase; Integrated Security=True"
- "Data Source=DNS-Name\\SQLEXPRESS; Initial Catalog=DemoDatabase; Integrated Security=True; TrustServerCertificate=True; Encrypt=False"
- "Data Source=DNS-Name\\SQLEXPRESS; Initial Catalog=DemoDatabase; User Id=SimpleUser; Password=SimpleUserPassword; TrustServerCertificate=True; Encrypt=False"
- "Data Source=DNS-Name\\SQLEXPRESS; Initial Catalog=DemoDatabase; Integrated Security=True"
- "Data Source=DNS-Name\\SQLEXPRESS; Initial Catalog=DemoDatabase; User Id=sa; Password=sapassword; TrustServerCertificate=True; Encrypt=False"
- "Data Source=IP-Address\\SQLEXPRESS; Initial Catalog=DemoDatabase; User Id=sa; Password=sapassword; TrustServerCertificate=True; Encrypt=False"
- "Server=.\\SQLEXPRESS; Database=DemoDatabase; User Id=SimpleUser; Password=SimpleUserPassword; TrustServerCertificate=True; Encrypt=False"


The json is read into the main MauiProgram.cs class and then the DbContextFactory is registered from it.

var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MauiProgram)).Assembly;

string strAppConfigStreamName = string.Empty;
strAppConfigStreamName = "MauiGoesEntity.appsettings.json";
var stream = assembly.GetManifestResourceStream(strAppConfigStreamName);

var config = new ConfigurationBuilder()
.AddJsonStream(stream)
.Build();

builder.Configuration.AddConfiguration(config);

var appSettings = System.Text.Json.JsonSerializer.Deserialize<appsettings>(assembly.GetManifestResourceStream(strAppConfigStreamName));
var connectionString = appSettings.ConnectionStrings.Default;
builder.Services.AddMauiBlazorWebView();

builder.Services.AddScoped<mauigoesentity.entity.data.orders.iauftraguebersichtservice, mauigoesentity.entity.data.orders.auftraguebersichtservice="">();

builder.Services.AddScoped<mauigoesentity.entity.data.idatabasecontext, mauigoesentity.entity.data.databasecontext="">();
builder.Services.AddDbContextFactory<mauigoesentity.entity.data.databasecontext>((DbContextOptionsBuilder options) =>
options.UseSqlServer(connectionString));
dolce_sweet 18-Sep-23 3:08am View    
Thank you very much for your help. I have checked all your suggestions and that was not the problem.
I have a link for you which will allow you to download my sample project (https://mycsharp.de/forum/posts/3840302). Search for "MauiGoesEntity.zip". This is from me.

Feel free to criticize the architecture of the sample project, but I don't spend too much time on the architecture of a sample project. It should simply represent in a lean way what my problem is. And in this example, it's about not being able to connect a database to the local SQL Server after a roll-out.
I am already happy to get a hint where I made a mistake / error in thinking, so I can fix it as soon as possible and close my knowledge gap.

Since I have not been working with MAUI for long, I ask for your indulgence.