john520

导航

 

 

 

         后面这个链接是mysql官方推荐的配置方法mysql 。 网址:  http://dev..com/doc/connector-net/en/connector-net-entityframework60.html

  我的做法:      我在VS2012中新建一个工程,之后项目的管理NuGet 程序包中通过联机下载两个安装包。

接下来在app.config中添加一些必要的配置文件如下:

<connectionStrings>
<add name="MyContext" providerName="MySql.Data.MySqlClient"
connectionString="server=localhost;port=3306;database=dbtest;uid=“你的用户名”;password=“你的密码”"/>
</connectionStrings>
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
</provider>
</providers>
</entityFramework>

接下来就可以写你的数据库架构了。我就写一个比较通用的一对多型的

 

public class Lesson
    {
        [Key]
        public int classId { get; set; }
        public string className { get; set; }

        public virtual List<student> students { get; set; }
    }
 public class student
    {
         [Key]
        public int Id { get; set; }
        public  string Name{ get; set; }
        public virtual Lesson lesson { get; set; } 
        public virtual List<Pen> Pens { get; set; } //一个学生可以有很多笔
    }
 
1 public class Pen
2     {
3         [Key]
4         public int penId { get; set; }
5         public string penName { get; set; }
6         public float Price { get; set; }
7         public virtual student student { get; set; } 
8 
9     }
  public  class MyContext: DbContext 
    {
        public MyContext(): base("name=MyContext")
        {
        }
        public DbSet<student> Student { get; set; } 
        public DbSet<Pen>Pen{ get; set; }
        public DbSet<Lesson> Lesson { get; set; } 
    }

这里的关系为课程对应多个学生,学生对应多只笔。这里的例子只是入门。

 class Program
    {
        static void Main(string[] args)
        {
            using (MyContext context = new MyContext())
            {
                context.Database.CreateIfNotExists();
                Pen p1 = new Pen();
                p1.penName = "铅笔";
                p1.Price = 2.7f;
                Pen p2 = new Pen();
                p2.penName = "钢笔";
                p2.Price = 4.5f;
                Lesson c1 = new Lesson { classId = 1 };
                c1.className = "数学";
                student s1 = new student { Id=1};
                s1.Name = "小明";
                context.Lesson.Add(c1);
                context.Student.Add(s1);
                context.Pen.Add(p1);
                context.Pen.Add(p2);
                p1.student = s1;
                p2.student = s1;
                s1.lesson = c1;
                context.SaveChanges();
            }
            
        }
    }

 

至于其中设置了classId为1,为什么表里面不是1,是由于我之前往里面插入两条数据,没有配置数据表结构时,默认为自增长形式。所以不理会你的赋值。具体怎么配置

数据库表结构有注解和FluentAPI两种方式。(第一次写博客,有错的或什么大家谅解,提醒我,- _-).

 

预告:下一篇将类的注解和FluentAPI对需要的数据库结构进行讲解

以下两份CodeFist 资料有需要的参考一下

链接:http://pan.baidu.com/s/1eRCIR9w 密码:p308

 

posted on 2016-04-02 18:50  john520  阅读(207)  评论(0)    收藏  举报