如何为此制作通用方法?

编程入门 行业动态 更新时间:2024-10-28 14:34:47
本文介绍了如何为此制作通用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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 generatedfrom any of the tables I imported using EF5.0. How would I abstract that?

JP Cowboy Coders Unite!

推荐答案

请参阅以下代码段。您还可以应用存储库模式并创建一个通用类来执行CRUD操作。请参阅我的博客链接。 ovaismehboob.wordpress/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. ovaismehboob.wordpress/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!

更多推荐

如何为此制作通用方法?

本文发布于:2023-10-28 17:40:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537321.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法

发布评论

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

>www.elefans.com

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