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. Oracle数据库定义语言(DDL)

    --使用Create遇见创建表 Create Table table_name ( column_name datatype [null|not null], column_name datatype ...

  2. ava:Map借口及其子类HashMap三

    ava:Map借口及其子类HashMap三 HashMap常用子类(异步非安全线程,性能高: Hashtable:同步的安全线程,性能低) map(HashMap)中的key,value可以通过 Se ...

  3. Android Studio Mac版快捷键

    mac上按键符号 ⌥ : option / alt ⇧ : shift ⌃ : control ⌘ : command ⎋ : esc (一)查找/查看相关 搜索任意内容 双击 sft 当前文件查找/ ...

  4. TCP/IP 详解卷一 - TCP CWR、ECE、URG、ACK、PSH、RST、SYN、FIN控制位

    from:https://blog.csdn.net/u012243115/article/details/43487461 2015年02月04日 15:56:32 阅读数:1464 TCP 和 U ...

  5. Python入门经典练习题

    [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? num_list=[]cou=0for i in range(1,5): for j in range( ...

  6. 【BZOJ 3238】差异 后缀自动机+树形DP

    题意 给定字符串,令$s_i$表示第$i$位开始的后缀,求$\sum_{1\le i < j \le n} len(s_i)+len(s_j)-2\times lcp(s_i,s_j)$ 先考虑 ...

  7. gcc 4.8.5安装

    在利用张乐博士的最大熵模型工具包(Maximum Entropy Modeling Toolkit for Python and C++)和条件随机场的经典工具包CRF++(CRF++: Yet An ...

  8. [原]NYOJ-字符串替换-113

    大学生程序代写 /*http://acm.nyist.net/JudgeOnline/problem.php?pid=113 字符串替换 时间限制:3000 ms  |  内存限制:65535 KB  ...

  9. 【caffe】卷积层代码解析

    1.Forward_cpu conv_layer.cpp template <typename Dtype> void ConvolutionLayer<Dtype>::For ...

  10. Error Domain=NSURLErrorDomain Code=-1202,Https服务器证书无效

    错误:“此服务器的证书无效.您可能正在连接到一个伪装成“www.xxxxxx.com”的服务器, 这会威胁到您的机密信息的安全 原因:安全证书是自建证书,没有得到认证. 解决方法: 1.导入NSURL ...