• EF Code-First 学习之旅 从已存在的数据库进行Code First


    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);
            }
        }
    }
  • 相关阅读:
    linux共享内存的命令
    css元素类型
    不争万年,只珍朝夕我对编程的态度
    Oracle读书笔记PL/SQL编程(二)之程序流程
    oracle读书笔记PL/SQL编程(一)之基本数据类型、程序结构
    关于解决oracle登录:ora12154:tns:无法解析指定的连接标识符
    Hibernate读书笔记hibernate第一个案例的分析
    Oracle读书笔记PL/SQL编程(三)之游标
    解决jscollpan不能出现水平滑动条的问题
    关于org.hibernate.exception.SQLGrammarException: could not insert:
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6641987.html
Copyright © 2020-2023  润新知