建表:

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

code first 尝试的更多相关文章

  1. Windows下构建ASP.NET Core+Code First+Docker

    背景介绍 本文将会示范如何在Windows系统下基于ASP.NET Core构建跨平台服务,并通过Docker容器运行发布. 首先说一下为什么选择这一套组合: 我本人和我们Code4Thought团队 ...

  2. Code First :使用Entity. Framework编程(5) ----转发 收藏

    第五章 对数据库映射使用默认规则与配置 到目前为止我们已经领略了Code First的默认规则与配置对属性.类间关系的影响.在这两个领域内,Code First不仅影响模型也影响数据库.在这一章,你将 ...

  3. NET Core+Code First+Docker

    NET Core+Code First+Docker背景介绍 本文将会示范如何在Windows系统下基于ASP.NET Core构建跨平台服务,并通过Docker容器运行发布. 首先说一下为什么选择这 ...

  4. 【译著】Code First :使用Entity. Framework编程(5)

    第五章 对数据库映射使用默认规则与配置 到目前为止我们已经领略了Code First的默认规则与配置对属性.类间关系的影响.在这两个领域内,Code First不仅影响模型也影响数据库.在这一章,你将 ...

  5. Centos 7 安装 Visual stdio Code

    最近微软正式发布了.net code 和asp.net code.尝试了下在linux下.net code和asp.net code使用. 具体怎么使用.net code 和asp.net code ...

  6. C#调试含有源代码的动态链接库遇见there is no source code available for the current location提示时的解决方案

    C#调试含有源代码的动态链接库遇见there is no source code available for the current location提示时的解决方案: 1.首先试最常规的方法:Cle ...

  7. 无法注册DLL/OCX:regsvr32 failed with exit code 0x5

    最近在服务器上装一个等值线控件,确报错: RegSvr32 failed with exit code 0x5   尝试解决方法1: 在cmd下 运行for %1 in (%windir%\syste ...

  8. iOS多线程编程Part 1/3 - NSThread & Run Loop

    前言 多线程的价值无需赘述,对于App性能和用户体验都有着至关重要的意义,在iOS开发中,Apple提供了不同的技术支持多线程编程,除了跨平台的pthread之外,还提供了NSThread.NSOpe ...

  9. WebAssembly:随风潜入夜

    What? WebAssembly 是一种二进制格式的类汇编代码,可以被浏览器加载和并进一步编译成可执行的机器码,从而在客户端运行.它还可以作为高级语言的编译目标,理论上任何语言都可以编译为 WebA ...

随机推荐

  1. Angularjs Scope 原型链

    我们知道scope是可以继承的.scope的继承和js原型链是同一个概念. <div ng-controller="parentCtrl"> {{name}} < ...

  2. buffer busy wait

    什么是buffer busy wait? A session that reads or modifies a buffer in the SGA must first acquire the cac ...

  3. Java---设计模块(单例的变形)(多例)

    设计模式1--单例变形(多例) ★ 缓存在单例中的使用 缓存在编程中使用很频繁,有着非常重要的作用,它能够帮助程序实现以空间换取时间,通常被设计成整个应用程序所共享的一个空间,现要求实现一个用缓存存放 ...

  4. css实现居中的各种方法

    css垂直居中有很多种方法,可以参考下这个网站

  5. DLL模块例2:使用__declspec(dllexport)导出函数,extern "C"规范修饰名称,隐式连接调用dll中函数

    以下内容,我看了多篇文章,整合在一起,写的一个例子,关于dll工程的创建,请参考博客里另一篇文章:http://www.cnblogs.com/pingge/articles/3153571.html ...

  6. Centos6 httpd与tomcat整合发布

    有时候需要在Web入口地址中隐藏非80端口,而最常见的方案是整合httpd和tomcat ,下面是通过mod_jk实现httpd和tomcat整合的详细流程,希望对大家有所帮助. OS: CentOS ...

  7. chinaOS

    从舆论上和政策规定上来推广操作系统,这对国产操作系统而言是好事,还是坏事? 从某种程度上来说,这是好事 运用行政的力量,率先在国家机关中普及国产操作系统确实是件好事,首先是确保了国家信息安全,其次则能 ...

  8. appium 并发测试

    Android并发测试 Appium提供了在一台设备上启动多个Android会话的方案,而这个方案需要你输入不同的指令来启动多个Appium服务来实现. 启动多个Android会话的重要指令包括: - ...

  9. 从epoll构建muduo-11 单线程Reactor网络模型成型

    mini-muduo版本传送门 version 0.00 从epoll构建muduo-1 mini-muduo介绍 version 0.01 从epoll构建muduo-2 最简单的epoll ver ...

  10. error userinfo error pos 5 友盟分享 网页分享(无新浪微博客户端)

    集成友盟分享的时候,其中微博分享,如果没有添加测试账号,就会出现{error userinfo error pos  5}的错误添加测试账号才能分享在测试信息下添加测试账号