如何创建可以使用任何EF Core dbContext的类库
我正在尝试创建一个可以消耗DBContext的类库,该库将能够查询具有常见模式的实体。 (这是一个数据库第一个项目)
我在班级库项目中有以下内容:
public interface ITranslate
{
public long Id
public long ParentId
public long LangId
}
在一个单独的项目中,我将有以下内容:
public partial class SectionTranslation : ITranslate
{
public string SectionName { get; set; }
{
在另一个项目中,我可能会拥有以下
public partial class TemplateTranslation : ITranslate
{
public string TemplateName { get; set; }
{
我想做的就是让以下合同但是,现实,我不确定如何处理这个。我希望能够返回与GetBestTranslation方法中常见查询相匹配的记录,我想返回截面转换或templateTrateTranslation实体,具体取决于项目在哪个项目中消耗了库以及Savetranslation方法,保存翻译记录。
public interface ITranslateManager
{
Task<T> GetBestTranslationAsync(long langId);
Task CreateTranslationAsync<T>(long langId)
}
谢谢大家的帮助
I am trying to create a class library that can consume a DbContext that will be able to query entities that have a common schema. (this is a DB first project)
I have the following in my class library project:
public interface ITranslate
{
public long Id
public long ParentId
public long LangId
}
In a seperate project, I would have the following:
public partial class SectionTranslation : ITranslate
{
public string SectionName { get; set; }
{
And in another project I might have the following
public partial class TemplateTranslation : ITranslate
{
public string TemplateName { get; set; }
{
What I would like to do is make the following contract a reality, however, I am not sure how to approach this one. I would want to be able to return a record that would match a common query in the GetBestTranslation method, I would want to return a SectionTranslation or TemplateTranslation Entity depending on which project is consuming the library and in the SaveTranslation method, save a translation record.
public interface ITranslateManager
{
Task<T> GetBestTranslationAsync(long langId);
Task CreateTranslationAsync<T>(long langId)
}
Thank you all in advance for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在方法上使用一些条件。
You can use a where condition on Method.
您可以通过这样做只需访问DBContext:
使用此上下文非常容易。您可以通过将传递到通用函数的
type
与现有数据集表type
匹配到上下文中的数据集。不要忘记添加wery:t:class
这将告诉该功能, typet
是由您提供的类分配的在通话中。如果您需要代码更具动态性,则可以将谓词添加为其参数。这将使您无需创建新功能即可使用其他查询。
例如
x =&gt; x == true
。要调用该方法,您必须告诉函数通用
类型
是什么。您这样做:You can simply access your dbContext by doing this:
Using this context is pretty easy. You can request a Dataset in your context by matching the
Type
passed into the generic function with an existing Dataset tableType
. Don't forget to addwhere : T : class
This will tell the function that the genericType
T
is assigned by the class that you provide in the call.If you need the code to be more dynamic, you can add a predicate as its parameter. This will allow you to use query different queries without having to create a new function.
For example
x => x == true
.To call the method you have to tell the function what the generic
Type
is. You do this like this: