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);
        }
    }
}
相關文章
相關標籤/搜索