23456789101112131415packages.config现实当前项目使用的package:12344、实例代码Domain中Category.cs1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;56namespaceNorthwind.Domain.Entities7{8publicclassCategory9{10///11///分类ID12///13publicintCategoryID{get;set;}1415///16///分类名称17///18publicstringCategoryName{get;set;}19}20}Data中NorthwindContext.cs1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;56usingSystem.Data.Entity;78usingNorthwind.Domain.Entities;910namespaceNorthwind.Data11{12publicclassNorthwindContext:DbContext13{14publicDbSetCategories{get;set;}15}16}App中Program.cs1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;56usingNorthwind.Data;7usingNorthwind.Domain.Entities;89namespaceNorthwind.App10{11classProgram12{13staticvoidMain(string[]args)14{15Categoryc=newCategory(){CategoryName="电子数码"};1617using(NorthwindContextdb=newNorthwindContext())18{19db.Categories.Add(c);20db.SaveChanges();21}2223Console.WriteLine("Finish");24Console.ReadKey();25}26}27}5、运行说明由于在上面的数据库连接字符串中并未包含指定的数据库名称,运行成功之后,将在本地数据引擎中创建如下数据库和表:数据库名称:Northwind.Data.NorthwindContext表名称:Categories6、示例代码附件-IMDN开发者社群-imdn.cn"> 23456789101112131415packages.config现实当前项目使用的package:12344、实例代码Domain中Category.cs1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;56namespaceNorthwind.Domain.Entities7{8publicclassCategory9{10///11///分类ID12///13publicintCategoryID{get;set;}1415///16///分类名称17///18publicstringCategoryName{get;set;}19}20}Data中NorthwindContext.cs1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;56usingSystem.Data.Entity;78usingNorthwind.Domain.Entities;910namespaceNorthwind.Data11{12publicclassNorthwindContext:DbContext13{14publicDbSetCategories{get;set;}15}16}App中Program.cs1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;56usingNorthwind.Data;7usingNorthwind.Domain.Entities;89namespaceNorthwind.App10{11classProgram12{13staticvoidMain(string[]args)14{15Categoryc=newCategory(){CategoryName="电子数码"};1617using(NorthwindContextdb=newNorthwindContext())18{19db.Categories.Add(c);20db.SaveChanges();21}2223Console.WriteLine("Finish");24Console.ReadKey();25}26}27}5、运行说明由于在上面的数据库连接字符串中并未包含指定的数据库名称,运行成功之后,将在本地数据引擎中创建如下数据库和表:数据库名称:Northwind.Data.NorthwindContext表名称:Categories6、示例代码附件 - IMDN开发者社群-imdn.cn">
登录
首页 » C# » EF Code First简介及一个入门级实例

EF Code First简介及一个入门级实例

于 2014-04-22 发布
0 134
下载积分: 1 下载次数: 0

代码说明:

一、EF Code First简介 EntityFramework 代码优先   二、EF Code First第一个简单实例 1、开发环境及数据库说明 开发环境:Visual Studio 2010 Ultimate sp1 Sql Server 2008 R2 数据库:Northwind 2、实例代码结构 结构说明: App:控制台应用程序 Data:数据访问 Domain:实体类 3、安装Entity Framework   在Visual Studio编辑器中点击Tools -> Library Package Manager -> Package Manager Console,在Package Manager Console窗口中执行下面语句,安装最新版Entity Framework。 PM> Install-Package EntityFramework   App层和Data层分别添加对EntityFramework的引用:     在App层安装EntityFramework之后,将自动添加App.config和packages.config文件。   App.config配置Entity Framework版本信息及数据库连接信息,修改其中数据连接信息以适应本地实际环境。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15   packages.config现实当前项目使用的package: 1 2 3 4 4、实例代码 Domain中Category.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Northwind.Domain.Entities 7 { 8 public class Category 9 { 10 /// 11      /// 分类ID 12      /// 13 public int CategoryID { get; set; } 14 15 /// 16      /// 分类名称 17      /// 18 public string CategoryName { get; set; } 19 } 20 } Data中NorthwindContext.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.Data.Entity; 7 8 using Northwind.Domain.Entities; 9 10 namespace Northwind.Data 11 { 12 public class NorthwindContext : DbContext 13 { 14 public DbSet Categories { get; set; } 15 } 16 } App中Program.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using Northwind.Data; 7 using Northwind.Domain.Entities; 8 9 namespace Northwind.App 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 Category c = new Category() { CategoryName = "电子数码" }; 16 17 using (NorthwindContext db = new NorthwindContext()) 18 { 19 db.Categories.Add(c); 20 db.SaveChanges(); 21 } 22 23 Console.WriteLine("Finish"); 24 Console.ReadKey(); 25 } 26 } 27 } 5、运行说明   由于在上面的数据库连接字符串中并未包含指定的数据库名称,运行成功之后,将在本地数据引擎中创建如下数据库和表:   数据库名称:Northwind.Data.NorthwindContext   表名称:Categories 6、示例代码附件

下载说明:请别用迅雷下载,失败请重下,重下不扣分!

发表评论

0 个回复

  • 物联网设备通讯协议实现客户端(IoTClient)
    这是一个物联网设备通讯协议实现客户端,将包括主流PLC通信读取、ModBus协议、Bacnet协议等常用工业通讯协议。 - 本组件基于.NET Standard 2.0,可用于.Net的跨平台开发,如Windows、Linux甚至可运行于树莓派上。 - 本组件终身开源免费,采用最宽松MIT协议,您也可以随意修改和商业使用(商业使用请做好评估和测试)。   - 开发工具:Visual Studio 2019  - QQ交流群:[995475200](https://jq.qq.com/?_wv=1027&k=5bz0ne5)   - IoTClient Tool [下载1](https://github.com/zhaopeiym/IoTClient/releases) [下载2](https://files-cdn.cnblogs.com/files/zhaopei/IoTClient.rar) 
    2021-05-06下载
    积分:1
  • C# 删除、遍历指定目录下的所有指定文件、文件夹
    C# 删除、遍历指定目录下的所有指定文件、文件夹
    2013-10-31下载
    积分:1
  • C#抽奖系统源码(适合新手学习)
    使用于各个场合的抽奖系统,代码中给出了详细注释,供大家学习,抽奖标题、抽奖内容、抽奖人员名单可自定
    2019-09-26下载
    积分:1
  • C# 仿QQ发送文件 含(客户端+服务端源码)
    C# 仿QQ发送文件 含(客户端+服务端源码)
    2015-09-16下载
    积分:1
  • WPF自定义的MessageBox
    WPF自定义的MessageBox
    2013-10-02下载
    积分:1
  • 微信框架 WeiXin.Framework 完整源码+实例 非常不错<赞>
    微信框架 WeiXin.Framework 完整源码+实例 非常不错
    2014-04-24下载
    积分:1
  • 通过c#控制origin编程
    通过c#控制origin编程,可以实现调用origin画图。
    2021-09-22 00:30:54下载
    积分:1
  • GC 垃圾回收算法
      =1,2垃圾回收算法:    托管堆:CLR要求的资源从托管堆分配,任何对象只要没有应用程序的根引用它,都会在某个时刻被垃圾回收器回收 基于代的机制,提高回收的性能,在程序的生命期中,新建的对象是新一代,而创建的比较早的对象是老一代,第0代是最近分配的对象,CLR 使用了0,1,2三代进行管理 =3 GC和调试GcAndDebug.cs =4本地资源终结(finalization)是CLR提供的一种机制,允许对象在垃圾回收器回收其内存之前执行一些得体的清理工作,任何包装了本地资源的类型都必须支持终结操作(实现一个命名为Finalize的方法)。GC判断一个对象是垃圾时,会调用对象的Finalize(实现的情况下),C#中使用~ClassName表示FinalizeIn most cases, you do not need to write classes that derive from the CriticalFinalizerObject class. The .NET Framework class library provides two classes, SafeHandle and CriticalHandle, that provide critical finalization functionality for handle resources. Furthermore, the .NET Framework provides a set of prewritten classes derived from the SafeHandle class, and this set is located in the Microsoft.Win32.SafeHandles namespace. These classes are designed to provide common functionality for supporting file and operating system handles. System.Runtime.ConstrainedExecution 下的 CriticalFinalizerObject 抽象对象,CLR特殊对待 System.Runtime.InteropServices下的派生抽象类 SafeHandle CriticalHandle 和SafeHandle只是引用计数的区别Microsoft.Win32.SafeHandles  SafeFileHandle,SafeRegistryHandle ,SafeWaitHandle... SafeProcessHandle SafeLibraryHandle SafeLocalMemHandle SafeThreadHandle等MSDN没有编写,但同样是处理这个问题的 类似的实现的不同类代表不同的资源类型,这么多的类似,主要是为了类型安全SafeHandle的作用:1、以前的IntPtr形式不够健壮,如在IntPtr赋值前可能抛出ThreadAbortException异常,这样托管代码将造成本地资源的泄露2、防止利用潜在的安全漏洞,一个线程访问一个本地资源,另一个线程释放该资源,通过引用计数实现 对托管资源只有在极少数情况下才使用终结器 终结器被调用的时刻:1、第0代满:2、显式调用GC.Collect3、Windows报告内存不足4、CLR卸载AppDomain5、CLR关闭 内部实现使用终结列表保留对象,通过这个GC进行处理, freachable队列 =Dispose终结器的调用时间由GC确定,调用者无法显式调用它Dispose提供了显式清理资源的能力 GCHandle WeakReference System.Runtime.CompilerServices.ConditionalWeakTable 加入时的是对象的WeakReference,因此可能会回收,但可以确保只要key存在,value也是存在的 任何大于85000字节的对象被自动认为是大对象,大对象在大对象堆中分配 ==大量资源GCpublic static void AddMemoryPressure(Int64 bytesAllocated);   public static void RemoveMemoryPressure(Int64 bytesAllocated);提示GC实际需要消耗的内存,然后GC监视内存压力,压力变大时,强制回收 System.Runtime.InteropServices  HandleCollector 数量有限的本地资源 MemoryFailPoint在需要大量内存前可以先检查是否可以使用这么大的内存 GC的监视代码形式: GC.GetTotalMemory GC.CollectionCountPerMon.exe系统性能监视工具CLR Profiler工具FUSLOGVW.execlrver.exeSvcTraceViewer.exe
    2012-12-16下载
    积分:1
  • c# 二十多种设计模式实例源码下载-完整版
    二十多种设计模式实例源码下载-完整版
    2013-08-30下载
    积分:1
  • c# 搜片神器源码
    c# 搜片神器源码
    2013-10-19下载
    积分:1
  • 696518资源总数
  • 105547会员总数
  • 4今日下载