C#.Net微说集.NET

学习篇二:Code First(实验)

2017-09-11  本文已影响39人  张中华

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


第二步:新建项目
EF6 构建结构


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; }

    }
}

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




那么第一步创建实体就完成了.现在就是第二步了.创建上下文.创建上下文我们就写一个上下的类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; }
    }
}

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

方式二:

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();
        }
    }

}

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

上一篇下一篇

猜你喜欢

热点阅读