![](/img/trans.png)
[英]How do I deploy ASP.NET boilerplate WebApi class library as a stand-alone web service?
[英]ASP.NET BoilerPlate: How do I inject object of DbContext in application service?
如何在應用程序服務中注入DbContext對象?
需要訪問它以創建實體記錄的克隆。
您可以將IDbContextProvider<TDbContext> _sampleDbContextProvider
用作構造函數注入,與_sampleDbContextProvider.GetDbContext();
一起使用_sampleDbContextProvider.GetDbContext();
克隆實體的一種棘手方法是先序列化然后反序列化對象。 使用Newtonsoft進行序列化。 只需使用此代碼
MyEntity myEntity = _myEntityRepository.get(1);
string cloned = JsonConvert.SerializeObject(myEntity);
MyEntity clonedEntity = JsonConvert.DeserializeObject<MyEntity>(cloned);
clonedEntity.Id = 0;
這個問題有點模棱兩可,但是您需要考慮以下幾點:
依賴注入 :AUTOFAC或NINJECT(例如)這將允許您通過ctor或可以使用它的屬性將 DbContext注入到任何類中。
將其作為參數傳遞 (您的服務如何生成?)
var appService = new ApplicationService(new MyDbContext())
其中任何一種都可以。 如果這不是您的意思,那么更多信息會有所幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.