zoukankan      html  css  js  c++  java
  • code first 尝试

    建表:

    1.先用EF连接数据库,配置connectionStrings

    <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>
    <connectionStrings>
    <add name="DefaultConnection" connectionString="data source=1XXXXXX0;initial catalog=Athena;user id=sa;password=XXXXXXX;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />

    </connectionStrings>

    2.编写实体类  这个自己定义

    3.增加Context   例如:

    public class MyContext : DbContext
    {

    public MyContext():base ("name=DefaultConnection")//这里的DefaultConnection与配置文件connectionStrings中的名称一致
    {
    }

    public DbSet<实体1> 实体名1 { get; set; }
    public DbSet<实体2>  实体名2 { get; set; }
    }

    4.添加到数据库 

    var d = DateTime.Now.Date.ToString("yyyyMM");
    var aa = new 实体1
    {
    //属性赋值
    };
    using (var context = new MyContext())
    {
    context.实体名1.Add(aa);
    context.SaveChanges();
    }
    Console.WriteLine("OK");

    执行成功上面第四步以后,可以到数据库查看数据是否添加成功

    -----------------------------------------------------------------割----------------------------------------------------------------------

    codefirst更新实体字段

    1.打开控制台。方式:VS.NET →“视图”工具栏→其它窗口→程序包管理器控制台

    2.运行命令Enable-Migrations

    The EntityFramework package is not installed on project 'Athena.ChemicalIndustrySystem.Wcf'.需要设置默认项目

    More than one context type was found in the assembly 'WebApplication1'.
    To enable migrations for 'WebApplication1.AthenaEntities', use Enable-Migrations -ContextTypeName WebApplication1.AthenaEntities.
    To enable migrations for 'WebApplication1.VIewModel.BreakAwayContext', use Enable-Migrations -ContextTypeName WebApplication1.VIewModel.BreakAwayContext.
    To enable migrations for 'WebApplication1.Models.ApplicationDbContext', use Enable-Migrations -ContextTypeName WebApplication1.Models.ApplicationDbContext.

    选择一个context进行设置

    改步骤成功提示:

    PM> Enable-Migrations -ContextTypeName WebApplication1.VIewModel.BreakAwayContext
    Checking if the context targets an existing database...
    Detected database created with a database initializer. Scaffolded migration '201603160144114_InitialCreate' corresponding to existing database. To use an automatic migration instead, delete the Migrations folder and re-run Enable-Migrations specifying the -EnableAutomaticMigrations parameter.
    Code First Migrations enabled for project WebApplication1.

    3. PM> Update-Database
    Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
    No pending explicit migrations.
    Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.

    在工程中Migrations下面设置Configuration中 AutomaticMigrationsEnabled = true;

    4. PM> Update-Database

    Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
    No pending explicit migrations.
    Applying automatic migration: 201603160157077_AutomaticMigration.
    Running Seed method.

    到此为止数据库Update成功

  • 相关阅读:
    mysql常用sql语句的练习笔记
    docker-compose使用--config启动mongodb出错的采坑记录
    ubuntu1804安装docker和docker-compose的最新2020详细教程
    ubuntu1804使用国内git源安装fastdfs的笔记
    2020最新nginx+gunicorn+supervisor部署基于flask开发的项目的生产环境的详细攻略
    2020年ubuntu1804安装php7.3最新详细教程
    2020年ubuntu1804安装nginx最新稳定版1.16详细教程笔记
    ubuntu1804python安装mysqlclient的模块报错的解决办法
    ubuntu1804开启mysql远程访问功能和设置root远程访问
    ubuntu1804使用python3 venv 创建虚拟目录和制定Pip国内安装源
  • 原文地址:https://www.cnblogs.com/hexinxiaoyao/p/5282447.html
Copyright © 2011-2022 走看看