学习篇二:Code First(实验)

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

}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,931评论 19 139
  • 前言 Contoso 大学示例 Web 应用程序演示如何使用实体框架(EF)Core 2.0 和 Visual S...
    程序员长春阅读 12,299评论 1 15
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,126评论 6 342
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,034评论 25 709
  • 这一周馨儿值周。值周是馨儿学校的安排,高年级的学生(四年级以上)轮流每周一个班,上学放学时在楼道和校门口做护卫,主...
    yanzuliu阅读 2,929评论 0 0

友情链接更多精彩内容