2025.05.23 C# TIPS
データソース
Form_Load イベントで、データグリッドビューにデータソースを設定した後、セルの書式設定などを続けて、Form_Load イベントで実行しても反映されない。
どうも、データグリッドビューにデータソースの設定が完全に終了するには、Form_Load イベントが終了後となる。
セルなどの書式せっては、Form_Shown で行う。
dotnet -ef コマンド
dotnet -ef コマンドを実行すると、~Context.cs ファイル内のデータベース接続情報が既定の書き方に設定される。独自に書き直すこともできる。
■既定の設定
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) #warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263. => optionsBuilder.UseSqlServer("Data Source=(local);Initial Catalog=TestDB;Integrated Security=true;TrustServerCertificate=True");
■変更
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { string Datasource = TestDB.Properties.Settings.Default.DataSource; string InitialCatalog = TestDB.Properties.Settings.Default.InitialCatalog; string UserId = TestDB.Properties.Settings.Default.UserID; string Password = TestDB.Properties.Settings.Default.Password; optionsBuilder.UseSqlServer($"Server={Datasource};Database={InitialCatalog};user id={UserId};password={Password};TrustServerCertificate=true"); }