日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

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

發布時間:2025/1/21 asp.net 84 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ADO.NET Entity Framework -Code Fisrt 开篇(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 ,完成!

轉載于:https://www.cnblogs.com/iampkm/archive/2012/12/25/2832472.html

總結

以上是生活随笔為你收集整理的ADO.NET Entity Framework -Code Fisrt 开篇(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。