EntityFramework Code-First 简易教程(十一)-------从已存在的数据库中映射出表

怎样从一个已存在的数据库中映射表到 entity 实体?数据库

Entity Framework 提供了一个简便方法,能够为已存在的数据库里的全部表和视图建立实体类(entity class),而且能够用 DataAnnotation 特性和 Fluent API 来配置。ide

首先,右键你的 Visual Studio 项目 -> 添加 -> 新建项目:ui

code first for an existing database

 

选择 ADO.NET实体数据模型(ADO.NET Entity Data Model ),并指定模型名称(这个名称将会是 contenxt 类的类名),点击添加。spa

code first for an existing database

 

而后将会打开 Entity数据模型(Entity Data Model)的向导页面,选择 Code first from database ,而后点击下一步。.net

code first for an existing database

 

若是下拉列表中没有你想包含的数据库,点击 New Connection 来为已存在的数据库建立一个数据库连接。选择好了,点击下一步。code

code first for an existing database

 

选择你想要映射成类的表或视图,点击完成。blog

code first for an existing database

 

而后EF就会建立全部你选择的表或视图的实体类。ip

code first for an existing database

 

以下所示,每当你映射一个数据库进来的时候,EF都会建立一个对应的 context 类(使用 Fluent API 配置)。 get

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);
        }
    }
}
相关文章
相关标签/搜索