2022年5月5日 星期四

dll 專案使用內建連線字串

不要依賴組態設定檔,因為參考的專案不會使用到,直接於程式內處理
雖然可以在參考專案的組態設定檔加入相同設定,但缺乏獨立性且麻煩,也會造成 .net 6.0 專案無法使用 .net 4.8 的dll,因為組態設定檔架構不同

1. 擴充 DbContext class,設定連線字串,並提供產生物件的函數
 public partial class xxx : System.Data.Entity.DbContext
    {
        static string ConnectionString = "metadata=xxx;provider=System.Data.SqlClient;provider connection string=\"xxx\"";
        private xxx(string connectionString)
       : base(connectionString)
        {
        }

        public static xxx CreateDbContext()
        {            
            return new xxx(ConnectionString);
        }
    }

2. 使用連線物件
using (var db = xxx.CreateDbContext()) {
}

沒有留言:

自訂權限驗證機制

// 使用 filter [Route("api/[controller]")] [ApiController] [Authorize] [TypeFilter(typeof(CustomAsyncAuthorizationFilter))] public c...