二、MVC3+EF单表增删改查

2018-06-22 07:18:52来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

document 表为例 写入静态类 NorthwindDataProvider:

Controller可直接调用:如

1 //获取document表全部数据
2 NorthwindDataProvider.GetDocuments()

document 类:EF 映射

 1     public static class NorthwindDataProvider
 2     {
 3         const string NorthwindDataContextKey = "DXNorthwindDataContext";
 4 
 5         public static NWindEntities DB
 6         {
 7             get
 8             {
 9                 if (HttpContext.Current.Items[NorthwindDataContextKey] == null)
10                     HttpContext.Current.Items[NorthwindDataContextKey] = new NWindEntities();
11                 return (NWindEntities)HttpContext.Current.Items[NorthwindDataContextKey];
12             }
13         }
14         /// <summary>
15         /// 查询所有document表数据
16         /// </summary>
17         /// <returns></returns>
18         public static IEnumerable GetDocuments()
19         {
20             return from document in DB.Documents select document;
21         }
22         /// <summary>
23         /// 增加一条document数据
24         /// </summary>
25         /// <param name="doc">传入新数据</param>
26         public static void AddDocument(Document doc)
27         {
28             DB.Documents.AddObject(doc);
29             DB.SaveChanges();
30         }
31         /// <summary>
32         /// 根据ID查询document数据
33         /// </summary>
34         /// <param name="id">传入主键</param>
35         /// <returns></returns>
36         public static Document GetDocumentById(int id)
37         {
38             var val = from document in DB.Documents where document.DocumentId == id select document;
39             return val.First();
40         }
41         /// <summary>
42         /// 修改一条document数据
43         /// </summary>
44         /// <param name="doc">传入修改后的数据</param>
45         public static void UpdateDocument(Document doc)
46         {
47             Document oldDoc = DB.Documents.First(p => p.DocumentId == doc.DocumentId);
48             oldDoc.DocumentCode = doc.DocumentCode == null ? oldDoc.DocumentCode : doc.DocumentCode;
49             oldDoc.DocumentName = doc.DocumentName == null ? oldDoc.DocumentName : doc.DocumentName;
50             oldDoc.DocumentMemo = doc.DocumentMemo == null ? oldDoc.DocumentMemo : doc.DocumentMemo;
51             oldDoc.DocumentType = doc.DocumentType == null ? oldDoc.DocumentType : doc.DocumentType;
52             oldDoc.DocumentDate = doc.DocumentDate == null ? oldDoc.DocumentDate : doc.DocumentDate;
53             DB.SaveChanges();
54         }
55         /// <summary>
56         /// 删除一条document数据
57         /// </summary>
58         /// <param name="id">被删除数据的主键</param>
59         public static void deleteDocument(int id)
60         {
61             var obj = DB.Documents.Where(c => c.DocumentId == id).First();
62             DB.DeleteObject(obj);
63             DB.SaveChanges();
64         }
65     }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:微软的深度学习框架cntk ,我目前见过 安装方式最简单的一个框架

下一篇:C#Execl