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成功

  • 相关阅读:
    pip命令提示unknow or unsupported command install解决方法
    SSH登录慢解方案
    python/shell脚本报异常^M: bad interpreter: No such file or directory
    MySQL中自增ID起始值修改方法
    Centos给文件设置了777权限仍不能访问解决方案
    CentOS7下安装mysql5.7
    CentOS7下使用yum安装MariaDB
    sublime text 3启动报错"swallow_startup_errors"解决方法
    一个清除数组的方法在 Kotlin、Java、C#和Nim上的性能测试
    国外立交桥设计参考资料
  • 原文地址:https://www.cnblogs.com/hexinxiaoyao/p/5282447.html
Copyright © 2011-2022 走看看