zoukankan      html  css  js  c++  java
  • 设置EntityFramework 在开发时自动更新数据库

    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);
            }
        }
    }
  • 相关阅读:
    J Identical Trees(求俩个树转化所需的最小代价,hash判同构,费用流求转移代价)
    I Tournament(俩人一组,构造最少排队总时间)
    D
    purfer序列(有关度数与生成树个数情况)
    博客园自定义样式
    莫比乌斯知识点整理
    随笔日记
    牛客小白月赛16
    Codeforces Round #555 (Div. 3)
    Codeforces Round #553 (Div. 2)
  • 原文地址:https://www.cnblogs.com/VirtualMJ/p/5178090.html
Copyright © 2011-2022 走看看