如何为此制作通用方法? [英] How to make a Generic method for this?
问题描述
public static int Insert(Model.ItemMaster entity) {
using (Model.Entities ae1 = new Model.Entities())
{
ae1.Set(typeof(Model.ItemMaster)).Add(entity);
return ae1.SaveChanges();
}
}
鉴于上面的代码,我想抽象这个插入方法。  但是我不想传递Model.ItemMaster类型的特定类作为实体,我想传入从使用EF5.0导入的任何表中生成的任何类。
我该如何抽象?
JP Cowboy Coders Unite!
Given the code above I am wanting to abstract this insert method. But I don't want to pass in a specific class of type Model.ItemMaster as the entity, I want to pass in any class that was generated from any of the tables I imported using EF5.0.
How would I abstract that?
JP Cowboy Coders Unite!
推荐答案
请参阅以下代码段。您还可以应用存储库模式并创建一个通用类来执行CRUD操作。请参阅我的博客链接。
http://ovaismehboob.wordpress.com/2012/10/13/implementing-repository-pattern-with-entity-framework-code-first-model-approach/
Please see the following code snippet. You can also apply the Repository pattern and make a generic class to perform CRUD operations. Please see my blog link as well. http://ovaismehboob.wordpress.com/2012/10/13/implementing-repository-pattern-with-entity-framework-code-first-model-approach/
public static int Insert<T>(T TObject) where T : class
{
Model.Entities ae1=new Model.Entities();
var newEntry = ae1.Set<T>().Add(TObject);
return ae1.SaveChanges();
}
希望有所帮助!
Hope it helps!
这篇关于如何为此制作通用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!