Entity Framework Code-First(14):From Existing DB
Code-First from an Existing Database:
Here, you will learn how to generate code-first context and entity classes for an existing database.
Entity Framework provides an easy way to use code-first approach for an existing database. It will create entity classes for all the tables & views in your existing database and configure it with DataAnnotations attributes and Fluent API.
To use code-first for an existing database, right click on your project in Visual Studio -> Add -> New Item..

Select ADO.NET Entity Data Model in the Add new item dialog box and specify model name (This will be a context class name) and click on Add.

This will open Entity Data Model wizard as shown below. Select Code first from database option and click Next.

Now, select data connection for existing database. Create new connection for your database if dropdown does not include connection to your existing database. Click Next to continue.

Now, choose tables and views for which you want to generate classes and click on Finish.

This will generate all the entity classes for your DB tables and views as shown below.

For example, it will create following context class which uses Fluent API to configure entity classes as per your database.
namespace EFDemo
{
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq; public partial class SchoolContext : DbContext
{
public SchoolContext()
: base("name=SchoolContext2")
{
} public virtual DbSet<Course> Courses { get; set; }
public virtual DbSet<Standard> Standards { get; set; }
public virtual DbSet<Student> Students { get; set; }
public virtual DbSet<StudentAddress> StudentAddresses { get; set; }
public virtual DbSet<Teacher> Teachers { get; set; }
public virtual DbSet<View_StudentCourse> View_StudentCourse { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>()
.Property(e => e.CourseName)
.IsUnicode(false); modelBuilder.Entity<Course>()
.HasMany(e => e.Students)
.WithMany(e => e.Courses)
.Map(m => m.ToTable("StudentCourse").MapLeftKey("CourseId").MapRightKey("StudentId")); modelBuilder.Entity<Standard>()
.Property(e => e.StandardName)
.IsUnicode(false); modelBuilder.Entity<Standard>()
.Property(e => e.Description)
.IsUnicode(false); modelBuilder.Entity<Standard>()
.HasMany(e => e.Students)
.WithOptional(e => e.Standard)
.WillCascadeOnDelete(); modelBuilder.Entity<Standard>()
.HasMany(e => e.Teachers)
.WithOptional(e => e.Standard)
.WillCascadeOnDelete(); modelBuilder.Entity<Student>()
.Property(e => e.StudentName)
.IsUnicode(false); modelBuilder.Entity<Student>()
.Property(e => e.RowVersion)
.IsFixedLength(); modelBuilder.Entity<Student>()
.HasOptional(e => e.StudentAddress)
.WithRequired(e => e.Student)
.WillCascadeOnDelete(); modelBuilder.Entity<StudentAddress>()
.Property(e => e.Address1)
.IsUnicode(false); modelBuilder.Entity<StudentAddress>()
.Property(e => e.Address2)
.IsUnicode(false); modelBuilder.Entity<StudentAddress>()
.Property(e => e.City)
.IsUnicode(false); modelBuilder.Entity<StudentAddress>()
.Property(e => e.State)
.IsUnicode(false); modelBuilder.Entity<Teacher>()
.Property(e => e.TeacherName)
.IsUnicode(false); modelBuilder.Entity<Teacher>()
.HasMany(e => e.Courses)
.WithOptional(e => e.Teacher)
.WillCascadeOnDelete(); modelBuilder.Entity<View_StudentCourse>()
.Property(e => e.StudentName)
.IsUnicode(false); modelBuilder.Entity<View_StudentCourse>()
.Property(e => e.CourseName)
.IsUnicode(false);
}
}
}
Entity Framework Code-First(14):From Existing DB的更多相关文章
- Entity Framework Tutorial Basics(14):Choose development approach
Choose development approach with Entity Framework: We have seen Code-first, Model-first and Database ...
- Entity Framework Code first(转载)
一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...
- Entity Framework Code First (三)Data Annotations
Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...
- Entity Framework Code First (二)Custom Conventions
---------------------------------------------------------------------------------------------------- ...
- Entity Framework Code First (一)Conventions
Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...
- Entity Framework Tutorial Basics(31):Migration from EF 4.X
Migration from Entity Framework 4.1/4.3 to Entity Framework 5.0/6.0 To migrate your existing Entity ...
- Entity Framework Tutorial Basics(11):Code First
Code First development with Entity Framework: Entity Framework supports three different development ...
- Entity Framework Code First (八)迁移 Migrations
创建初始模型和数据库 在开始使用迁移(Migrations)之前,我们需要一个 Project 和一个 Code First Model, 对于本文将使用典型的 Blog 和 Post 模型 创建一个 ...
- Entity Framework Code First (七)空间数据类型 Spatial Data Types
声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...
随机推荐
- python 3 mysql 单表查询
python 3 mysql 单表查询 1.准备表 company.employee 员工id id int 姓名 emp_name varchar 性别 sex enum 年龄 age int 入职 ...
- Linux 中 bashrc 中的 rc 是什么意思
刚刚配置 .bashrc 文件时,突然想,这里面的 rc 是什么意思? 使用了这么长时间,这个都不知道,这是醉了.Google 之,还真有不少人探究过,哈哈. 这个 ubuntu 中文论坛里,有人回答 ...
- POJ 1611并查集
我发现以后写题要更细心,专心! #include<iostream>#include<algorithm>#include<stdio.h>#include< ...
- spring boot项目遇到 'lower_case_table_names' 的解决办法
今天自己搭建了spring boot项目,配置的是mysql数据库,启动时报如下错误 Mon Jan 22 23:31:40 CST 2018 WARN: Establishing SSL conne ...
- cdoj 1256 昊昊爱运动 预处理
昊昊爱运动 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 昊昊喜欢运动 他NN ...
- 初学Linux笔记
自动获取IP地址的局域网中,用的是DHCP服务器
- Java微信开发_00_资源汇总贴
1.微信公众平台技术文档(https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432) 2.微信企业号开发接口文档(ht ...
- java-StringBuffer常用方法
对字符串进行修改的时候,需要使用可变长字符串StringBuffer 和 StringBuilder 类. append(String s):将指定的字符串追加到此字符序列. Reverse():将此 ...
- nodejs stream基础知识
分类 nodejs 的 stream 有四种: Readable:可读流 Writable: 可写流 Duplex:双工流 Transform:转换流 Readable // _read方法是从底层系 ...
- stl_queue.h
stl_queue.h // Filename: stl_queue.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: http:/ ...