您的位置:首页 > 其它

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

2012-12-25 15:13 211 查看

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 ,完成!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: