• 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文件夹,在里面添加如下几个类文件:

    image

    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 运行程序,成功后,将在数据库中自动创建好数据库表结构.

    image

    Item 表

    image 

    OK ,完成!

  • 相关阅读:
    html JS 开发备忘
    C++学习备忘(一)
    博客开通备忘
    自己制作的代码生成工具AutoCoder
    C# 小技巧
    突破list存为模板为10M限制
    开博
    OpenEuler中C语言中的函数调用测试
    socket测试3
    电子公文传输系统验收4开发基础
  • 原文地址:https://www.cnblogs.com/iampkm/p/2832472.html
Copyright © 2020-2023  润新知