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成功
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 内存占用高分析
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· .NET开发智能桌面机器人:用.NET IoT库编写驱动控制两个屏幕
· 用纯.NET开发并制作一个智能桌面机器人:从.NET IoT入门开始
· 一个超经典 WinForm,WPF 卡死问题的终极反思
· 支付宝事故这事儿,凭什么又是程序员背锅?有没有可能是这样的...
· 在线客服系统 QPS 突破 240/秒,连接数突破 4000,日请求数接近1000万次,.NET 多
· C# 开发工具Visual Studio 介绍
· 在 Windows 10 上实现免密码 SSH 登录
· C#中如何使用异步编程