.NET EF contextfactory 通过库共享

发布于 2025-01-11 11:36:51 字数 917 浏览 0 评论 0原文

我有一个项目,其中有带有 dbsets 等的 MyDbContext 类。我有一个库项目,其中有一些在我的项目中共享的实用程序。

我也想在库中使用 MyDbContext 。但对于每个项目来说,dbcontext 在逻辑上都是不同的。我想通过属性 IDbContextFactory 的接口来完成此操作。但是这个接口需要泛型类型(DbContext),但它在库中不存在。

我试图找到一些解决方案,但我不知道该怎么做。

谢谢您的建议。

[编辑]

这是在我的应用程序中

public partial class TestRepository 
{
   public TestRepository(IDbContextFactory<MyDbContext> con) 
   {
   }
}

,我将在库中使用类似的东西。但我不能在那里使用 MyDbContext 因为那里不存在。所以我希望我会使用引用 IDbContextFactory 的接口来完成此操作,但我需要泛型类型...

// Not working in library
public partial class TestRepository 
{
   public TestRepository(IDbContextFactory<MyDbContext> con) 
   {
   }
}

// Maybe works but I don't how do it
public partial class TestRepository 
{
   public TestRepository(IFrameworkDbContextFactory con) 
   {
   }
}

I have my project where I have MyDbContext class with dbsets etc. And I have a library project where I have some utilities shared over my projects.

I would like use MyDbContext in the library, too. But for every project the dbcontext is logically another. I wanted do it over interface where will be property IDbContextFactory. But this interface needs generic type (DbContext) but it doesn't exist in the library.

I tried to find some solution but I don't know how to do it.

Thank you for your advice.

[Edit]

This is in my application

public partial class TestRepository 
{
   public TestRepository(IDbContextFactory<MyDbContext> con) 
   {
   }
}

And I would be use something like this in library. But I cannot use there MyDbContext because there doesn't exist. So I hope I will do it with interface where was reference to IDbContextFactory but I need generic type...

// Not working in library
public partial class TestRepository 
{
   public TestRepository(IDbContextFactory<MyDbContext> con) 
   {
   }
}

// Maybe works but I don't how do it
public partial class TestRepository 
{
   public TestRepository(IFrameworkDbContextFactory con) 
   {
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

庆幸我还是我 2025-01-18 11:36:51

也许这可以帮助你。

在库crete接口中

public interface ISharedDbContextFactory : IDbContextFactory<DbContext> {}

在项目中实现它

public class SharedDbContextFactory : ISharedDbContextFactory {
   private IDbContextFactory<MyDbContext> _contextFactory;

   public SharedDbContextFactory(IDbContextFactory<MyDbContext> contextFactory) {
       _contextFactory = contxtFactory;
   }

   public MyDbContext CreateDbContext()
   {
       return _contextFactory.CreateDbContext();
   }
}

然后将其添加到di配置中。您还可以仅注册到 IDbContextFactory 并使用它。我认为最好有您的自定义界面。

希望我给了你一些建议,没有说错什么。有人可以纠正我。

Maybe this can help you.

In library crete interface

public interface ISharedDbContextFactory : IDbContextFactory<DbContext> {}

In project impement it

public class SharedDbContextFactory : ISharedDbContextFactory {
   private IDbContextFactory<MyDbContext> _contextFactory;

   public SharedDbContextFactory(IDbContextFactory<MyDbContext> contextFactory) {
       _contextFactory = contxtFactory;
   }

   public MyDbContext CreateDbContext()
   {
       return _contextFactory.CreateDbContext();
   }
}

And then add it to di configuration. You can also register to di only IDbContextFactory and use it. I think it's better to have your custom interface.

I hope I gave you some advice and didn't say anything wrong. Someone can correct me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文