MySQL[转][EF在VS2010蒙利用Entity framework与MySQL

正文转自:http://www.cnblogs.com/kesalin/archive/2012/03/09/entityframework.html

在VS2010中应用Entity framework与MySQL

罗朝辉 (http://www.cnblogs.com/kesalin/)

正文遵循“签-非经贸用途-保持一致”创作公用协议

 

本文讲述了于VS2010惨遭以EF与MySQL的一个简短示例。

工具安装:

1,MySQL

MySQL Community Server 

Connector/NET
6.3.5

MySQL Workbench

个别下充斥者的老三个软件,注意:VS2010时支持的无限好的凡 Connector/NET
6.3.5,下载其他版本可能用更进一步的改动配置,最好安装者版。然后依次安装,注意修改MySQL的初始密码并切记。

 

2,确认安装了 ADO.NET Entity Data Model

右击已有的C#工程,然后选取 Add New Item,在 Visual C#
Items->Data栏目中看看有没有起ADO.NET Entity Data
Model选项,如果无则还并未安装该模板;或你为堪当VS安装目录下看看C:\Program
Files\Microsoft Visual Studio
10.0\Common7\IDE\ItemTemplates\CSharp\Data\1033\AdoNetEntityDataModelCSharp.zip
该公文是无有,如果无有则要安装该模板。如何设置也,插入VS安装盘,选择修复,选中该零件安装就足以了。

 

3,使用MySQL workbench在 MySQL表中新建数据库
EFSample,在里头新建表customer,该表包含三独字段 id, Address,
Name。如下所示:

MySQL 1

 

4,新建C#控制台程序 EFSample,然后右击工程称为,然后选取 Add New Item,在
Visual C# Items->Data中选择ADO.NET Entity Data Model,命名为
EFSampleModel.edmx:

MySQL 2

 

 然后选打数据库生成:

MySQL 3

 

下一场设置New Connection,选择MySQL Provider,如下所示:

MySQL 4

 

分选而投的数据库暨发明,并以模型命名为EFSampleModel:

MySQL 5

 

至今,我们即便能在工程中观看创建的EFSampleModel.edmx:

MySQL 6

 

5,我们可以利用xml编辑打开 EFSampleModel.edmx,在这文件中定义了SSDL
content,CSDL content以及 C-S mapping
content。同样,EF为我们自动生成了同数据库对于的运作时类,这些近似让定义在EFSampleModel.Designer.cs文件中,其中起针对customer类的定义。该类定义了一个工措施,我们可下该工厂方法来生成
customer 对象。

MySQL 7😉

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
 [EdmEntityTypeAttribute(NamespaceName="EFSampleModel", Name="customer")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class customer : EntityObject
    {
        #region Factory Method

        /// <summary>
        /// Create a new customer object.
        /// </summary>
        /// <param name="address">Initial value of the Address property.</param>
        /// <param name="id">Initial value of the id property.</param>
        /// <param name="name">Initial value of the Name property.</param>
 public static customer Createcustomer(global::System.String address, global::System.Int64 id, global::System.String name)
        {
            customer customer = new customer();
            customer.Address = address;
            customer.id = id;
            customer.Name = name;
            return customer;
        }

        #endregion
    }

MySQL 8😉

 

6,新建 customer的组成部分近似来丰富对象的叙说:

MySQL 9😉

namespace EFSample
{
    public partial class customer : global::System.Data.Objects.DataClasses.EntityObject
    {
        override public string ToString()
        {
            return string.Format("Customer Name: {0}, Address: {1}", this.Name, this.Address);
        }
    }
}

MySQL 10😉

 

7,支持大部分配置工作完成,下面开始编制和数据库进行交互相关的代码。修改
Program.cs为:

MySQL 11😉

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace EFSample
{
    class Program
    {
        const int MaxRow = 10;

        static void DeleteData()
        {
            using (var ctx = new EFSampleEntities())
            {
                var customers = from c in ctx.customer select c;
                foreach (customer c in customers)
                {
                    ctx.DeleteObject(c);
                }

                ctx.SaveChanges();
            }
        }

        static void InsertData(customer[] cs)
        {
            using (var ctx = new EFSampleEntities())
            {
                foreach (customer c in cs)
                {
                    ctx.AddTocustomer(c);
                }

                ctx.SaveChanges();
            }
        }

        static void QueryData()
        {
            using (var ctx = new EFSampleEntities())
            {
                for (int i = 1; i <= MaxRow; i++)
                {
                    String str = i.ToString();
                    var results = ctx.customer.Where(c => c.Address == str);
                    foreach (customer c in results)
                    {
                        Console.WriteLine(c);
                    }
                }
            }
        }

        static void Main(string[] args)
        {
            customer[] cs = new customer[MaxRow];
            for (int i = 1; i <= MaxRow; i++)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("用户");
                sb.Append(i);
                customer c = customer.Createcustomer(i.ToString(), i, sb.ToString());

                cs[i - 1] = c;
            }

            Console.WriteLine("=================== TEST START ===================");

            DeleteData();

            Console.WriteLine(">> Storage test start...");
            Stopwatch sw = Stopwatch.StartNew();

            InsertData(cs);

            sw.Stop();
            Console.WriteLine("<< Store data seconds: " + sw.ElapsedMilliseconds / 1000 + " ( " + sw.ElapsedMilliseconds + " miliseconds)");

            Console.WriteLine(">> Query test start...");
            sw = Stopwatch.StartNew();

            QueryData();

            sw.Stop();
            Console.WriteLine("<< Query data seconds: " + sw.ElapsedMilliseconds / 1000 + " ( " + sw.ElapsedMilliseconds + " miliseconds)");

            Console.WriteLine(">> Delete test start...");
            sw = Stopwatch.StartNew();

            DeleteData();

            sw.Stop();
            Console.WriteLine(">> Delete data seconds: " + sw.ElapsedMilliseconds / 1000 + " ( " + sw.ElapsedMilliseconds + " miliseconds)");

            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
        }
    }
}

MySQL 12😉

 

8,测试。在方的代码中,有三独援方法插入数据,查询数据,删除数据。然后在
main()函数中分头调用它们,并对准性进行开估算。当我们才插入10记录时,其结果如下:

MySQL 13

 

 

网站地图xml地图