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 233
下载积分: 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 个回复

  • 利用c#写的jpg拼图软件,可以随意剪切和拼接(钩子源码)
    利用c#写的jpg拼图软件,可以随意剪切和拼接using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;using System.Runtime.InteropServices;namespace AutoDrawRect{ public class MouseHook { //好吧这个没有用到 private bool isSet; public bool IsSet { get { return isSet; } } //这个也没有用到 private int handleOfHook; public int HandleOfHook { get { return handleOfHook; } } //这个还是没有用到、、、淡定! private bool isStopMsg; public bool IsStopMsg { get { return isStopMsg; } set { isStopMsg = value; } } //自己定义了一个事件 放到Hook里面去 public delegate void MEventhandler(object sender, MouseInfoEventArys e); public event MEventhandler HooKMouseEvent; [DllImport("user32.dll")]//设置钩子 第二个参数为回调函数指针 public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hmod, int dwThreadid); [DllImport("user32.dll")]//传递到下一个钩子 public static extern int CallNextHookEx(int hHook, int nCode, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll")]//卸载钩子 public static extern bool UnhookWindowsHookEx(int hHook); [DllImport("kernel32.dll")]//获取模块句柄 public static extern IntPtr GetModuleHandle(string lpModuleName); public const int WH_MOUSE_LL = 14;//全局鼠标Hook 7是局部的 13全局键盘 2局部键盘 public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);//话说c#里面委托就是函数指针?、 private const int WM_LBUTTONDOWN = 0x201; //在Hook里面判断是否左键点下 private const int WM_RBUTTONUP = 0x205; //在Hook里面判断是否右键抬起 public struct POINT {//鼠标位置的结构体 public int x; public int y; } public struct MouseLLInfo {//全局鼠标Hook的结构体 public POINT pt; //其实这里可以用Point只是这个新建的类里面没有应用System.Windows.Forms(应该是这个) public int mouseData; public int flags; public int time; public int dwExtraInfo; } GCHandle gc;//好吧 话说就是应为这个东西害得我研究了两天 没有这个的话 程序运行一会儿就提示崩溃了 //因为垃圾回收期把我的回调函数当垃圾收了 所以运行程序的时候 一会儿就提示我 一个垃圾的回调导致程序崩溃 //在非托管调用托管的时候 必须保持托管代码的或活动性 大概就这个意思 反正就是被收废品的收了、害的我用.net3.5用其他方式设置Hook public int MouseHookProcedure(int nCode, IntPtr wParam, IntPtr lParam) {//这个就是回调函数了 if (nCode >= 0 && HooKMouseEvent != null) {//先判断是否事件被绑定(感觉有点多余的判断 丫的我不在上面绑定 我写Hook干嘛) //话说是把内存的什么什么转换成结构体 MouseLLInfo mouseInfo = (MouseLLInfo)Marshal.PtrToStructure(lParam, typeof(MouseLLInfo)); Btn btn = Btn.None; //自己定义的一个枚举 里面只有三个值 if (wParam == (IntPtr)WM_LBUTTONDOWN) { //如果左键被点下 btn = Btn.LeftDowm; } else if (wParam == (IntPtr)WM_RBUTTONUP) { //如果右键被抬起 btn = Btn.RightUp; } //好吧 我就不知道当时我怎么想的 在Hook里面获取的坐标 有负数的现象 所以在那边 我没用这个坐标 MouseInfoEventArys e = new MouseInfoEventArys(btn, mouseInfo.pt.x, mouseInfo.pt.y); HooKMouseEvent(this, e);//触发绑定到这个上面的事件 } return CallNextHookEx(handleOfHook, nCode, wParam, lParam);//继续下一个钩子 } public bool SetMouseHook() { //设置Hook if (isSet) {//如果已经设置了 就不要设置啦、、、 return false; } HookProc MouseCallBack = new HookProc(MouseHookProcedure); handleOfHook = SetWindowsHookEx(WH_MOUSE_LL, MouseCallBack, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0); if (handleOfHook != 0) {//如果设置成功、、 gc = GCHandle.Alloc(MouseCallBack);//这个就是那个什么什么、、然后我的回调就不会被收废品的拣去了 isSet = true; return true; } return false; } public bool UnLoadMouseHook() { if (!isSet) {//如果装都没有装那么久不要卸载啦、、 return false; } if (UnhookWindowsHookEx(handleOfHook)) { gc.Free();//将回调释放掉、、 isSet = false; return true; } return false; } } public enum Btn//我只感觉到这三个有用、(应该是两个 左键点下 右键抬起) { LeftDowm, RightUp, None } public class MouseInfoEventArys {//话说定义事件的时候都是这么写的 所以我也弄一个内出来保存事件参数 private int x;//坐标 多余的后来才发现 鼠标慢慢贴近屏幕边缘的时候 3 2 1 0 -1 、、丫的 负数都出来了 public int X { get { return x; } } private int y;//坐标 public int Y { get { return y; } } private Btn mBtn; public Btn MBtn { get { return mBtn; }//鼠标的情况 } public MouseInfoEventArys(Btn btn,int x,int y) {//构造器 mBtn = btn; this.x = x; this.y = y; } }}
    2013-12-22下载
    积分:1
  • 提取exe dll文件中的 资源图片文件例子源码下载
    提取exe dll文件中的 资源图片文件例子源码下载
    2015-04-29下载
    积分:1
  • C# QQ自动注册机 实例源码
    自动填表,获取验证码,保持cookie
    2014-11-27下载
    积分:1
  • Winform treeview控件设计及应用源码
    C#winform 控件开发 TreeView实例
    2017-04-28下载
    积分:1
  • WPF很炫的几何图形 实例源码下载
    左侧导航移动上去有滑动效果(不错),右侧弹出效果更炫,下载即可使用
    2013-10-03下载
    积分:1
  • Modbus连接上位机,接收数据
    连接上位机,接受数据
    2020-11-28下载
    积分:1
  • C# 数据排序 示例代码
    C# 数据排序 示例代码
    2015-04-22下载
    积分:1
  • C# 创建三角网项目(AutoCAD)
    C# 创建三角网项目(AutoCAD)
    2019-05-28下载
    积分:1
  • P+F r2000 sample
    R2000-UHD demo
    2020-02-20下载
    积分:1
  • unit3d 塔防游戏源码
    unit3d 塔防游戏源码
    2013-09-12下载
    积分:1
  • 696516资源总数
  • 106914会员总数
  • 0今日下载