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);
}
}
}

设置EntityFramework 在开发时自动更新数据库的更多相关文章

  1. 如何设置“用eclipse开发时自动在顶端产生import”?

    eclipse新版本中的默认设置可能导致自动在生成import时是在代码中,而大多数时间我们需要自动生成import在代码顶端,按照如下设置即可纠正

  2. 解决SpringBoot+JPA中使用set方法时自动更新数据库问题

    项目进行了三分之二了,突然出现一个很诡异的bug,数据库存储的用户表中密码信息总是自动消失一部分,头疼了几天后突然想起同事有个对低权限用户查询的用户信息向前台传送时会把密码设成null后再传输,心想是 ...

  3. Code First 下自动更新数据库结构(Automatic Migrations)

    示例 Web.config <?xml version="1.0" encoding="utf-8"?> <configuration> ...

  4. EntityFrameworkCore使用Migrations自动更新数据库

    EntityFrameworkCore使用Migrations自动更新数据库 系统环境:Win10 IDE:VS2017 RC4 .netcore版本:1.1 一.新建ASP.NET Core Web ...

  5. winform datagridview 不显示默认第一列 不显示未绑定列 数据源发生改变时自动更新 (转)

    不显示带星号的第一列: datagridview属性框中将 RowHeadersVisiber 设置为  false 不显示未绑定列: datagridview有一个属性是 AutoGenerateC ...

  6. EF修改model自动更新数据库

    最近用MVC+EF学习时遇到修改model后而数据库没更新报错,就在网上找关于数据迁移自动更新数据库的,折腾了大半天终于弄了出来 第一步:在程序包管理器控制台里: Enable-Migrations ...

  7. day35-hibernate映射 03-Hibernate持久态对象自动更新数据库

    持久态对象一个非常重要的能力:自动更新数据库. package cn.itcast.hibernate3.demo1; import static org.junit.Assert.*; import ...

  8. 扩展BindingList,防止增加、删除项时自动更新界面而不出现“跨线程操作界面控件 corss thread operation”异常

    在做界面程序时,常常需要一些数据类,界面元素通过绑定等方式显示出数据,然而由于UI线程不是线程安全的,一般都需要通过Invoke等方式来调用界面控件.但对于数据绑定bindingList而言,没法响应 ...

  9. T450设置插入USB鼠标时自动禁用触摸板

    刚入手T450,打字时经常碰到触摸板,很是恼火,于是求助万能的度娘,找了卡饭基佬的教程,实测可行,大家可以试试.<win7下如何设置插入USB鼠标时自动禁用触摸板>,地址:www.kafa ...

随机推荐

  1. Android 状态栏通知Notification、NotificationManager简介

    Notification(通知)一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个通知: 在Android系统中, ...

  2. HDU4453--Looploop (Splay伸展树)

    Looploop XXX gets a new toy named Looploop. The toy has N elements arranged in a loop, an arrow poin ...

  3. PHP常用魔术方法(__toString魔术方法)

    <?php //文件名:index.php define('a',__DIR__); include '/IMooc/Loader.php'; spl_autoload_register('\\ ...

  4. jquery.tochart.js

    var _jq, _hc; var jqsrc = "http://code.jquery.com/jquery-1.7.min.js"; var hcsrc = "ht ...

  5. Windows下命令行直接编译程序

    D:\> cl hello.cpp Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86 Cop ...

  6. linux高级技巧:heartbeat+lvs(一)

    1.heartbeat一个简短的引论:        Heartbeat 项目是 Linux-HA project的一个组成部分,它实现了一个高可用集群系统.心跳服务和集群通信是高可用集群的两个关键组 ...

  7. 内存测试工具memtester

    Memtester是用户态工具,用于测试内存子系统的故障.非常方便,支持32位 或64位Unix-like系统.对于硬件开发开发者来说,memtester可以定位到物理地址. 1. 安装 下载地址ht ...

  8. neural style论文解读

    相关的代码都在Github上,请参见我的Github,https://github.com/lijingpeng/deep-learning-notes 敬请多多关注哈~~~ 概述 在艺术领域,艺术家 ...

  9. 转载: Javah生成JNI头文件出现找不到类的错误

    错误: 找不到 'com.chnic.jni.SayHellotoCPP' 的类文件. 上图可以看到错误和解决办法. 不要忘记那个点 javah -classpath . -jni com.chnic ...

  10. 网页搜索功能 多表搜索sql

    SELECT ID, Title, FromTableFROM (SELECT ID, ArticleName AS Title, 'Article' AS FromTable        FROM ...