学习篇二:Code First(实验)

mvc 4 + EF 6实现code first。
第一步:建立一个测试数据库,并添加表和两条数据:

2789632-a45c7627673dd832.png

第二步:新建项目
EF6 构建结构
2789632-b4f11de50de1c59d.png

2789632-f600b904826385d9.png

2789632-54c712493af5ca5b.png

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hello
{
    public class Hello
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }

    }
}

以下是处理表关联的两种方式:


2789632-81bb1647c69e1000.png

2789632-4ff3ec74967eb076.png

那么第一步创建实体就完成了.现在就是第二步了.创建上下文.创建上下文我们就写一个上下的类HelloDbContext.cs:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hello
{
    class HelloDbContext:DbContext
    {
        public HelloDbContext()
            : base("name=Hello")//与配置文件对应
        { 
            
        }
        public DbSet<Hello> Hello { get; set; }
    }
}

在主函数里创建一个数据库,给表添加点字段,测试一下成功没。
失败!

方式二:

2789632-5732c9b7f79c0b06.png

app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="HelloEntities" connectionString="Data Source=localhost;port=3306;Initial Catalog=helloweb;user id=root;password=0301;" providerName="MySql.Data.MySqlClient"/>
  </connectionStrings>
</configuration>

MyContext.cs

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestOne
{
    //public class HelloDbContext:DbContext
    //{
    //    public HelloDbContext() : base("name=HelloEntities") { }
    //    public DbSet<HelloTable> HelloTable { get; set; }
    //}
    public class MyContext : DbContext
    {
        public MyContext()
            : base("name=HelloEntities")
        {
        }
        public DbSet<hellotable> hellotables { get; set; }
    }
    public class hellotable
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>());
            var context = new MyContext();
            context.hellotables.Add(new hellotable {Name = "55" });
            context.SaveChanges();
        }
    }

}

之后再做分离吧,晚安,做个好梦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值