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的更多相关文章

  1. Entity Framework Tutorial Basics(14):Choose development approach

    Choose development approach with Entity Framework: We have seen Code-first, Model-first and Database ...

  2. Entity Framework Code first(转载)

    一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...

  3. Entity Framework Code First (三)Data Annotations

    Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...

  4. Entity Framework Code First (二)Custom Conventions

    ---------------------------------------------------------------------------------------------------- ...

  5. Entity Framework Code First (一)Conventions

    Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...

  6. 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 ...

  7. Entity Framework Tutorial Basics(11):Code First

    Code First development with Entity Framework: Entity Framework supports three different development ...

  8. Entity Framework Code First (八)迁移 Migrations

    创建初始模型和数据库 在开始使用迁移(Migrations)之前,我们需要一个 Project 和一个 Code First Model, 对于本文将使用典型的 Blog 和 Post 模型 创建一个 ...

  9. Entity Framework Code First (七)空间数据类型 Spatial Data Types

    声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...

随机推荐

  1. OC实现将N个数随机排列

    + (NSMutableArray *)randArray : (NSMutableArray *)arrayM { NSMutableArray *resultM = [[NSMutableArra ...

  2. 常用JQuery设置HTML元素内容

    主要内容: 一.获取内容及属性 二.设置内容及属性 三.添加元素 四.删除元素 五.css()方法 六.寻找祖先及后代 一.获取内容及属性 二.设置内容及属性 相对于获取内容及属性的方式,只需在函数内 ...

  3. EntityFramework 学习 一 Delete Entity using DBContext in Disconnected Scenario

    Student studentToDelete; . Get student from DB using (var ctx = new SchoolDBEntities()) { studentToD ...

  4. C程序员必须知道的内存知识【英】

    C程序员必须知道的内存知识[英] 时间 2015-03-08 14:16:11  极客头条原文  http://marek.vavrusa.com/c/memory/2015/02/20/memory ...

  5. Java 面试题问与答:编译时与运行时

    Java 面试题问与答:编译时与运行时 2012/12/17 | 分类: 基础技术, 职业生涯 | 5 条评论 | 标签: RUNTIME, 面试 分享到:58 本文作者: ImportNew - 朱 ...

  6. php简单实现通讯录采集,我的第一个php,适合新手

    起源于要整理通讯录,原先用的是文件调查,现在学了php,就自己试一下.程序短小精悍,适于学习.有两个文件,bj.html用于显示和采集表单信息.bj.php用于处理数据和反馈结果.突出之处在于可以上传 ...

  7. PHP中有多态么

    PHP中有多态么 一.总结 一句话总结:封装是类的构建过程,php具有:php也具有继承的特性.唯独这个多态,php体现的十分模糊.原因是php是弱类型语言. php不具有像java那种清晰的多态,不 ...

  8. 时间复杂度O(n)与空间复杂度O(1)

    把输入规模看成x轴,所花时间/空间看成y轴.O(n)就是 y = x, y随x的增长而线性增长.一条斜线O(1)就是 y = 1,不管x如何变,y不变.一条与x平行的线 举个简单的例子,要从0加到n, ...

  9. leetcode 162 Find Peak Element(二分法)

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  10. Struts2 - 文件的上传和下载

    1.  前言 这个章节是Struts2框架应用最广泛的三个版块(上传下载.国际化.校验输入)之一,所以这一版块的学习还蛮重要的. 2.  具体内容 回到顶部 2.1Struts2文件上传 2.1.1单 ...