使用EF数据库第一种方法创建通用抽象类

编程入门 行业动态 更新时间:2024-10-27 06:29:37
本文介绍了使用EF数据库第一种方法创建通用抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一些类似的实体,我想为它们为 CRUD 操作创建一个通用的抽象类,但是我不知道如何访问泛型类中的实体。例如:

I have some similar entities and I want to create a generic abstract class for CRUD operations for them, but I don't know how can access to entities in generic class. For example:

public abstract class HeaderRepository<T> { public HeaderRepository() { } public virtual byte[] Insert(T item) { using (MyEntities context = new MyEntities()) { context.***** <-- What I should write to add an object to entity T } } }

我希望我明白了。

谢谢

Thanks

推荐答案

您需要设置您的数据库集。最好在类级别有一个私有变量,在构造函数中设置,然后使用它。

You need to set your DB set. It would be better to have a private variable at class level, set in the constructor and then use it.

using (MyEntities context = new MyEntities()) { DbSet<T> dbSet = context.Set<T>(); dbSet.Add(...) //Whatever your operation is. }

更多推荐

使用EF数据库第一种方法创建通用抽象类

本文发布于:2023-05-29 23:13:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/355162.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:种方法   数据库   抽象类   EF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!