ADO.NET Entity Framework -Code Fisrt 开篇(一)
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
引用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 开篇(一)的更多相关文章
- ADO.NET Entity Framework
ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案, 早期被称为 ObjectSpace,现已经包含在 V ...
- 使用 Entity Framework Code First
使用 Entity Framework Code First 在家闲着也是闲着,继续写我的[ASP.NET MVC 小牛之路]系列吧.在该系列的上一篇博文中,在显示书本信息列表的时候,我们是在程序代码 ...
- 读书笔记之ado.net entity framework
提供了对数据访问的一种抽象层,是更加易于以编程的方式来操作及管理数据 有以下几种模式:Model First, Database First, and Code First 现在主要讨论code Fi ...
- 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 ...
- 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 ...
- ADO.NET-EF:ADO.NET Entity Framework 百科
ylbtech-ADO.NET-EF:ADO.NET Entity Framework 百科 ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 ...
- Entity Framework Code First学习系列目录
Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity Framework 5.0+MS SQL Server 2012, ...
- ADO.NET Entity Framework CodeFirst 如何输出日志(EF 5.0)
ADO.NET Entity Framework CodeFirst 如何输出日志(EF4.3) 用的EFProviderWrappers ,这个组件好久没有更新了,对于SQL执行日志的解决方案的需求 ...
- Entity Framework Code First数据库连接
1. 安装Entity Framework 使用NuGet安装Entity Framework程序包:工具->库程序包管理器->程序包管理器控制台,执行以下语句: PM> Insta ...
随机推荐
- 某模拟题(USACO部分题+noip2005部分题)
题目描述 农场上有N(1 <= N <= 50,000)堆草,放在不同的地点上.FJ有一辆拖拉机,也在农场上.拖拉机和草堆都表示为二维平面上的整数坐标,坐标值在1..1000的范围内.拖拉 ...
- mac下的一些操作
mac 下修改Hosts文件 : http://www.cnblogs.com/zhangqs008/p/3773623.html mac下装Tomcat服务器: 在苹果系统安装Tomcat:首先下载 ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(尺取)题解
题意:n个城市,初始能量c,进入i城市获得a[i]能量,可能负数,去i+1个城市失去b[i]能量,问你能不能完整走一圈. 思路:也就是走的路上能量不能小于0,尺取维护l,r指针,l代表出发点,r代表当 ...
- yum第三方安装-软件包没签名及更新错误
yum安装时 后面加 --nogpgcheck 阿里云源文件:http://mirrors.aliyun.com/repo/Centos-7.repo epel repo源:http://mirror ...
- 【Streaming】30分钟概览Spark Streaming 实时计算
本文主要介绍四个问题: 什么是Spark Streaming实时计算? Spark实时计算原理流程是什么? Spark 2.X下一代实时计算框架Structured Streaming Spark S ...
- Package.json 属性说明
name - 包名. version - 包的版本号. description - 包的描述. entry pointer 项目入口文件 没有的直接回车跳过 test command: 测试命令 后面 ...
- maven 插件在线安装
NO.1 在Eclipse中安装Maven插件安装详解 前言 本来是没打算写博客的,作为一个13年毕业的菜鸟,自认为水平太渣写不出什么好文章,但是前些日子看到一篇鼓励性质的文章说,技术人员的成长靠的就 ...
- hdoj-1005-Number Sequences
题目:Number Sequences 代码: #include<stdlib.h> #include<iostream> #include<cstdio> #in ...
- 学习mybatis-3 step by step 篇三
动态 SQL if choose (when, otherwise) trim (where, set) foreach 动态 SQL 通常要做的事情是有条件地包含 where 子句的一部分.比如: ...
- H5图片预览、压缩、上传
目标实现: 1.选择图片, 前端预览效果 2.图片大于1.2M的时候, 对图片进行压缩 3.以表单的形式上传图片 4.图片删除 预览效果图: 代码说明: 1.input:file选择图片 <!- ...