惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
有赞技术团队
有赞技术团队
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Check Point Blog
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
The Cloudflare Blog
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
Vercel News
Vercel News
IT之家
IT之家
MyScale Blog
MyScale Blog
博客园_首页
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
罗磊的独立博客

博客园 - 小马过河MJ

Identity Server introspect 调用 /connect/introspect windows forget jenkins password. 转载(Asp.net Core 中试使用ZKWeb.System.Drawing) EFCore & Mysql migration on Production HTML to PDF pechkin Log4net 为MVC 添加下载权限 辞职 MiniProfiler Android Studio 设置LogCat 颜色 运用Swagger 添加WebAPI 文档 给现有MVC 项目添加 WebAPI Summernote async await 跨域调用WebApi MVC 伪静态 PagedList.MVC 应用 MVC 自定义错误处理 SQL Server 索引结构及其使用(四)[转] SQL Server 索引结构及其使用(三)[转]
设置EntityFramework 在开发时自动更新数据库
小马过河MJ · 2016-02-02 · via 博客园 - 小马过河MJ

1. NuGet 下载EntityFramework.

2. 定义Context 和 打开NuGet 命令 执行 Enable-Migrations , Libaray.DAL.Migrations.Configuration 要在执行命令后自动产生.

using Libaray.Models.Entities;
using Libaray.Models.Maps;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Libaray.DAL
{
    public class LibarayContext : DbContext
    {
        public LibarayContext(): base("name = LibCon")
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<LibarayContext, Libaray.DAL.Migrations.Configuration>()); //重要
        }

        public DbSet<UserModel> UserModels { get; set; }
        public DbSet<UserInRoleModel> UserInRoleModels { get; set; }
        public DbSet<UserRoleModel> UserRoleModels { get; set; }
        public DbSet<BookModel> BookModels { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Configurations.Add(new UserInRoleModelMap());
            modelBuilder.Configurations.Add(new UserRoleModelMap());
            modelBuilder.Configurations.Add(new UserModelMap());
            modelBuilder.Configurations.Add(new BookModelMap());
        }
    }
}

3. 修改configuration

namespace Libaray.DAL.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<Libaray.DAL.LibarayContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(Libaray.DAL.LibarayContext context)
        {

        }
    }
}

4. 实体类例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Libaray.Models.Entities
{
    public class BookModel
    {
        public Guid BookId { get; set; }
        public string BookName { get; set; }
        public string Author { get; set; }
        public string BookDescription { get; set; }
        public DateTime BookDate { get; set; }
        public DateTime CreatedOn { get; set; }
        public Guid CreatedBy { get; set; }
        public DateTime UpdatedOn { get; set; }
        public Guid UpdatedBy { get; set; }
    }
}

5. 实体类Mapping

using Libaray.Models.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Libaray.Models.Maps
{
    public class BookModelMap : EntityTypeConfiguration<BookModel>
    {
        public BookModelMap()
        {
            this.HasKey(u => u.BookId);
            this.Property(u => u.BookId).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
        }
    }
}