ADO.NET Entity Framework -Code Fisrt 开篇(一)

2012-12-25 15:13 by 易code, 911 阅读, 0 评论, 收藏, 编辑

ADO.NET Entity Framework 是微软的一套实体映射框架。发布EF4.1(Entity Framework )时,又提出了代码先行的设计理念(the code comes first, the rest follows)。具体好处哪是多多,查资料吧。

参考资料:Programming Entity Framework Code First.pdf

开发环境:VS2010

开发版本:ADO.NET Entity Framework 4.1

下载链接:http://download.microsoft.com/download/0/7/A/07AC6336-D665-4442-B841-39D11BBF2563/EntityFramework41.exe

引用dll:方法一:安装下载的exe文件,安装文件内有一个EntityFramework.dll 文件。 项目中需要引用该dll文件。

方法二: 在VS2010 中新建一个项目,在引用处选择 Add Libraray Package Reference  ,在左边选择 online,搜查Entity Framework  安装。

下面是Code Fisrt 的快速开始。

1 新建一个控制台项目QuickStart。添加一个Model文件夹,在里面添加如下几个类文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
namespace QuickStart.Model
{
    /// <summary>
    /// 统一字典表
    /// </summary>
   public class Dictionary
    {

public string DictionaryID { get; set; }
       public string DictionaryValue { get; set; }
       public string ParentID { get; set; }
       public string Parameter { get; set; }
       public DateTime LastUpdateTime { get; set; }
       public string Remark { get; set; }
    }
}

//字典表中保存了每一个Item 的分类信息,字典表分类和Item 是一对多关系

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations; //数据注释(需要添加引4.0版本)
namespace QuickStart.Model
{
   public class Item
    {
        public string ItemID { get; set; }
        public string Name { get; set; }
       public decimal Price { get; set; }      
       public Dictionary ItemType { get; set; }
    }
}

2  添加一个DBContextAPI  继承自DbContext 类,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity; //添加引用
using QuickStart.Model;
namespace QuickStart
{
   public class DBContextAPI :DbContext
    {
       /// <summary>
        /// 通过构造函数定义配置文件中使用的链接字符串name="OrderDB"
        /// <para>否则采用默认,name="DBContextAPI" 类名</para>
       /// </summary>
       public DBContextAPI() : base("OrderDB") { }

public IDbSet<Item> Items { get; set; }
       public IDbSet<Dictionary> Dictionarys { get; set; }
          }
}

3 添加一个app.config 配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="OrderDB" providerName="System.Data.SqlClient"
         connectionString="server=.;uid=sa;pwd=123456;database=OrderDB"/>
  </connectionStrings>
</configuration>

4 在main 函数中添加如下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuickStart.Model;
//using Microsoft.SqlServer.Server;
namespace QuickStart
{
    class Program
    {
        static void Main(string[] args)
        {
           // 测试,并自动创建数据库表模型
            CreateDataBase();
            Console.ReadKey();
        }

private static void CreateDataBase()
        {
            using (var db = new DBContextAPI())
            {
                var dict = new Dictionary()
                {
                    DictionaryID = "20121225001",
                    DictionaryValue = "笔记本电脑",
                    Parameter = "",
                    ParentID = "ItemType",
                    Remark = "笔记本电脑分类Key",
                    LastUpdateTime = DateTime.Now
                };
                db.Dictionarys.Add(dict);
                int result = db.SaveChanges();
                Console.WriteLine("追加{0}条记录成功!", result);
            }
        }
    }
}

5 运行程序,成功后,将在数据库中自动创建好数据库表结构.

Item 表

OK ,完成!

ADO.NET Entity Framework -Code Fisrt 开篇(一)的更多相关文章

  1. ADO.NET Entity Framework

    ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案, 早期被称为 ObjectSpace,现已经包含在 V ...

  2. 使用 Entity Framework Code First

    使用 Entity Framework Code First 在家闲着也是闲着,继续写我的[ASP.NET MVC 小牛之路]系列吧.在该系列的上一篇博文中,在显示书本信息列表的时候,我们是在程序代码 ...

  3. 读书笔记之ado.net entity framework

    提供了对数据访问的一种抽象层,是更加易于以编程的方式来操作及管理数据 有以下几种模式:Model First, Database First, and Code First 现在主要讨论code Fi ...

  4. SQLITE WITH ENTITY FRAMEWORK CODE FIRST AND MIGRATION

    Last month I’ve a chance to develop an app using Sqlite and Entity Framework Code First. Before I st ...

  5. APS.NET MVC + EF (02)---ADO.NET Entity FrameWork

    2.1 Entity Framework简介 Ado.net Entity Framework 是Microsoft推出的ORM框架. 2.1.1 什么是ORM 对象关系映射(Object Relat ...

  6. ADO.NET-EF:ADO.NET Entity Framework 百科

    ylbtech-ADO.NET-EF:ADO.NET Entity Framework 百科 ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 ...

  7. Entity Framework Code First学习系列目录

    Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity Framework 5.0+MS SQL Server 2012, ...

  8. ADO.NET Entity Framework CodeFirst 如何输出日志(EF 5.0)

    ADO.NET Entity Framework CodeFirst 如何输出日志(EF4.3) 用的EFProviderWrappers ,这个组件好久没有更新了,对于SQL执行日志的解决方案的需求 ...

  9. Entity Framework Code First数据库连接

    1. 安装Entity Framework 使用NuGet安装Entity Framework程序包:工具->库程序包管理器->程序包管理器控制台,执行以下语句: PM> Insta ...

随机推荐

  1. VC++开机自动启动程序的几种方法 (转载)

    转载:http://blog.csdn.net/zhazhiqiang/article/details/51909703 很多监控软件要求软件能够在系统重新启动后不用用户去点击图标启动项目,而是直接能 ...

  2. 常用maven命令总结

    常用Maven命令: mvn -v //查看版本 mvn archetype:create //创建 Maven 项目 mvn compile //编译源代码 mvn test-compile //编 ...

  3. SRLTE,SGLTE,SVLTE,CSFB,VoLTE的区别【转】

    本文转载自:https://blog.csdn.net/dangbochang/article/details/43851979 SRLTE——Single Radio LTE,俗称单待LTE. SG ...

  4. 啤酒和饮料|2014年蓝桥杯B组题解析第一题-fishers

    啤酒和饮料|2014年第五届蓝桥杯B组题解析第一题-fishers 啤酒和饮料 啤酒每罐2.3元,饮料每罐1.9元.小明买了若干啤酒和饮料,一共花了82.3元. 我们还知道他买的啤酒比饮料的数量少,请 ...

  5. github issues的操作

    https://help.github.com/en/articles/searching-issues-and-pull-requests 根据 reporter筛选issues https://h ...

  6. 安卓开发 UI入门

    布局的类型 线性布局 LinearLayout ***  垂直显示 vertical 水平显示 horizontal 文本适应 wrap_content 填充父窗体 match_parent 权重 l ...

  7. java对象在内存的大小

    前言 一直以来,对java对象大小的概念停留在基础数据类型,比如byte占1字节,int占4字节,long占8字节等,但是一个对象包含的内存空间肯定不只有这些. 假设有类A和B,当new A()或者n ...

  8. java必背面试题

    JAVA必背面试题和项目面试通关要点 一 数据库 1.常问数据库查询.修改(SQL查询包含筛选查询.聚合查询和链接查询和优化问题,手写SQL语句,例如四个球队比赛,用SQL显示所有比赛组合:举例2:选 ...

  9. MongoDB(课时14 正则运算)

    3.2.4.9 正则运算 如果想实现模糊查询,必须使用正则表达式,而且正则表达式使用的语言是Perl兼容的正则表达式的形式. 要实现正则使用,则按照如下的定义格式: 基础语法:{key : 正则标记} ...

  10. [ios]关于gps以及坐标系

    参考:http://mobile.51cto.com/iphone-387413.htm 美国GPS使用的是WGS84的坐标系统,以经纬度的形式来表示地球平面上的某一个位置,这应该是国际共识.但在我国 ...