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. P3768 【简单的数学题】

    P3768 [简单的数学题] \(Ans=\sum ^{n}_{i=1}\sum ^{n}_{j=1}ijgcd(i,j)\) \(=\sum ^{n}_{i=1}\sum ^{n}_{j=1}ij\ ...

  2. android OTG【转】

    本文转载自:http://blog.csdn.net/xubin341719/article/details/7707056 一.OTG的概念 OTG是On-The-Go的缩写,是近年发展起来的技术, ...

  3. Get Docker CE for Ubuntu

    Docker 分为开源免费的 CE(Community Edition)版本和收费的 EE(Enterprise Edition)版本. 配置 Docker 的 apt 源 1. 安装包,允许 apt ...

  4. Fidder工具抓包及篡改数据

    下载fiddler的最新版本: 运行fiddler之后测试要调试的页面是否可以捕获,刷新页面后左边列表会实时显示目前http请求的条目.如图红色部分 测试成功,开始断点捕获数据 点击菜单栏按钮[Rul ...

  5. LINQ 学习路程 -- 查询操作 Aggregate

    聚合操作执行数学的运算,如平均数.合计.总数.最大值.最小值 Method Description Aggregate 在集合上执行自定义聚集操作 Average 求平均数 Count 求集合的总数 ...

  6. 【leetcode刷题笔记】Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  7. FJOI2016 神秘数

    题目大意 给定长为$N$一个序列,每次询问一个区间,求最小的不能表示为由区间内若干个(可以是$0$个)数的和的非负整数. 考虑一个可重集合$S$,设抽取$S$中若干个数相加无法得到的最小非负整数为$A ...

  8. 反编译工具Reflector下载(集成FileGenerator和FileDisassembler)

    Reflector是一款比较强大的反编译工具,相信很多朋友都用过它,但reflector本身有很多局限性, 比如只能一个一个的查看方法等,但幸好reflector支持插件功能目前网上有很多reflec ...

  9. C++11中的原子操作(atomic operation)

    所谓的原子操作,取的就是“原子是最小的.不可分割的最小个体”的意义,它表示在多个线程访问同一个全局资源的时候,能够确保所有其他的线程都不在同一时间内访问相同的资源.也就是他确保了在同一时刻只有唯一的线 ...

  10. Python之常用模块(二)

    shelve xml处理 configparser hashlib logging   shelve模块 shelve是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持 ...