.NET EF contextfactory 通过库共享
我有一个项目,其中有带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许这可以帮助你。
在库crete接口中
在项目中实现它
然后将其添加到di配置中。您还可以仅注册到 IDbContextFactory 并使用它。我认为最好有您的自定义界面。
希望我给了你一些建议,没有说错什么。有人可以纠正我。
Maybe this can help you.
In library crete interface
In project impement it
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.