EntityFramework 7 Migrations 迁移命令
2023-02-18 15:32:26 时间
示例代码:
using Microsoft.Data.Entity;
using System.Collections.Generic;
namespace ClassLibrary1
{
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=DESKTOP-2P9GHDD\SA;Database=BloggingContextDb;User ID=sa;Password=123456;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Make Blog.Url required
modelBuilder.Entity<Blog>()
.Property(b => b.Url)
.IsRequired();
}
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
}
project.json 配置代码:
{
"version": "1.0.0-*",
"description": "ClassLibrary1 Class Library",
"authors": [ "xishuai" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"frameworks": {
"dnx451": { }
},
"dependencies": {
"EntityFramework.Core": "7.0.0-rc1-final",
"EntityFramework.Commands": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final"
},
"commands": {
"ef": "EntityFramework.Commands"
}
}
命令行转到程序目录,运行 dnx ef
,如果出现“找不到命令”提示,先运行 dnvm upgrade
。
dnvm upgrade
dnu restore
cd src\MyProject
dnx ef
dnx ef migrations add MyMigration
dnx ef database update
dnx ef
命令:
![](https://images2015.cnblogs.com/blog/435188/201511/435188-20151125174021499-78961014.png)
dnx ef migrations add MyMigration
命令:
![](https://images2015.cnblogs.com/blog/435188/201511/435188-20151125174033765-1042652774.png)
dnx ef database update
命令:
![](https://images2015.cnblogs.com/blog/435188/201511/435188-20151125174042265-2141236526.png)
dnx ef migrations --help
帮助:
database
update
–Updates the database to a specified migration
dbcontext
list
–List your DbContext typesscaffold
–Scaffolds a DbContext and entity type classes for a specified database
migrations
add
–Add a new migrationlist
–List the migrationsremove
–Remove the last migrationscript
–Generate a SQL script from migrations
参考资料:
相关文章
- 还重构?就你那代码只能铲了重写!
- 工作3年,看啥资料能月薪30K?
- 组内分享,画架构图的一些知识整理
- 给你一台服务器,你能把你写的代码部署到线上吗?
- 刚火了的中台转头就拆,一大波公司放不下又拿不起来!
- 工作两年简历写成这样,谁要你呀!
- 讲道理,只要你是一个爱折腾的程序员,毕业找工作真的不需要再花钱培训!
- 刚毕业不久,接私活赚了2万块!
- 北漂码农的我,把在大城市过成了屯子一样舒服,哈哈哈哈哈!
- 码农会锁,synchronized 对象头结构(mark-word、Klass Pointer)、指针压缩、锁竞争,源码解毒、深度分析!
- 都说搭博客简单,鬼知道后端程序员要经历什么!
- 炸!1024我的故事,一个写了两年博客的大厂码农!
- 几百行代码写个Mybatis,原理搞的透透的!
- 每个程序员都该有个自己的博客,分享我的四种博客搭建教程!
- 一次代码评审,差点过不了试用期!
- 数学,离一个程序员有多近?
- 程序员为什么热衷于造轮子,升职加薪吗?
- 牛掰,在IDEA中,你可以安装小傅哥写的插件了!
- 得物(毒)APP,8位抽奖码需求,这不就是产品给我留的数学作业!
- 90%的程序员,都没用过多线程和锁,怎么成为架构师?