欢迎访问 生活随笔!

尊龙游戏旗舰厅官网

当前位置: 尊龙游戏旗舰厅官网 > 编程语言 > asp.net >内容正文

asp.net

ado.net entity framework -尊龙游戏旗舰厅官网

发布时间:2025/1/21 asp.net 23 豆豆
尊龙游戏旗舰厅官网 收集整理的这篇文章主要介绍了 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
{
    ///


    /// 统一字典表
    ///

   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
    {
       ///


        /// 通过构造函数定义配置文件中使用的链接字符串name="orderdb"
        /// 否则采用默认,name="dbcontextapi" 类名
       ///

       public dbcontextapi() : base("orderdb") { }

        public idbset items { get; set; }
       public idbset dictionarys { get; set; }
          }
}

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



 
             connectionstring="server=.;uid=sa;pwd=123456;database=orderdb"/>
 

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 开篇(一)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得尊龙游戏旗舰厅官网网站内容还不错,欢迎将尊龙游戏旗舰厅官网推荐给好友。

网站地图