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

 

转载于:https://www.cnblogs.com/hexinxiaoyao/p/5282447.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值