建表:數據庫
1.先用EF鏈接數據庫,配置connectionStringsapp
<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" />ide
</connectionStrings>工具
2.編寫實體類 這個本身定義ui
3.增長Context 例如:spa
public class MyContext : DbContext
{code
public MyContext():base ("name=DefaultConnection")//這裏的DefaultConnection與配置文件connectionStrings中的名稱一致
{
}orm
public DbSet<實體1> 實體名1 { get; set; }
public DbSet<實體2> 實體名2 { get; set; }
}ip
4.添加到數據庫 ci
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成功