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 ...
随机推荐
- Linux slab分配器【转】
本文转载自:https://www.ibm.com/developerworks/cn/linux/l-linux-slab-allocator/ 良好的操作系统性能部分依赖于操作系统有效管理资源的能 ...
- win7 64位debug解决方法
1.下载win 64位的DOSBox,如DOSBox0.74: 2.下载win 32 debug.exe,并复制到调用的目录,如d盘根目录d:\ 3.安装DOSBox,并运行:如下图: 4.键入命令: ...
- ReentrantReadWriteLock分析
ReentrantReadWriteLock会使用两把锁来解决问题,一个读锁,一个写锁 线程进入读锁的前提条件: 没有其他线程的写锁, 没有写请求或者有读请求,但调用线程和持有锁的线程是同一个 线程进 ...
- JavaScript:正则表达式 分组
在现在的我看来,带小挂号的就是分组,嗯. 代码: var reg=/(abc)/; var str="abcdabcdeabcdef"; console.dir(reg.exec( ...
- python ros 关闭节点
def myhook(): print "shutdown time!" rospy.on_shutdown(myhook) 或 rospy.signal_shutdown(rea ...
- [ios]iOS8 定位
参考:http://www.2cto.com/kf/201410/342392.html http://blog.csdn.net/yongyinmg/article/details/39521523 ...
- JPA实体关系映射:@ManyToMany多对多关系、@OneToMany@ManyToOne一对多多对一关系和@OneToOne的深度实例解析
JPA实体关系映射:@ManyToMany多对多关系.@OneToMany@ManyToOne一对多多对一关系和@OneToOne的深度实例解析 今天程序中遇到的错误一 org.hibernate.A ...
- [.NET开发] C# 读写文件
1.C#读文件 按行读取文件: public void Read(string path) { StreamReader sr = new StreamReader(path,Encoding.Def ...
- 通过修改manifest文件来解决Vista/Win7/Win8/win10下应用程序兼容性问题
https://www.cnblogs.com/snowbook/p/5363990.html
- vs.net应用程序图标以及制作安装程序快捷方式图标设置
我们通常在开发软件完毕后需要打包制作安装程序,这个时侯一般都会需要设置应用程序的图标,使用默认的图标一般都比较难看,那么我们应该怎样设置自定义的图标呢? 我现在要讲的图标设置有两种情况, ...