建表:
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成功