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);
}
}
}

EF Code-First 学习之旅 从已存在的数据库进行Code First的更多相关文章

  1. itext poi 学习之旅 (3)读取数据库信息并由excel展现出来

    DBConnection.java 连接数据库操作 package com.zkbj.poi; import java.sql.Connection; import java.sql.DriverMa ...

  2. Entity Framework Code First 学习日记(1)精

    我最近几天正在学习Entity Framework Code First.我打算分享一系列的学习笔记,今天是第一部分: 为什么要使用Code First: 近 年来,随着domain driven d ...

  3. 开发了5年android,我开始了go学习之旅

    前言 做了近5年的android开发,最近项目也是不怎么忙,空闲的时候总会思考一些事情,不过作为移动开发,我个人觉得很有必要学习后台开发,由于公司是Go语言开发的,了解go语言一段时间后,我发现go语 ...

  4. Code First is a bad name,这些年我们对Code First的理解都错了 !很震惊吧?

    当看到这个时,我也很震惊.估计绝大多数的人和我一样,这些年来,一直不知道Code Fisrt的真实意义.下面是一篇讲述此情况的译文,欢迎围观,若有翻译不当的地方,请指正,谢谢.如果被惊到了,请点赞!, ...

  5. EF Code First学习笔记

    EF Code First学习笔记 初识Code First EF Code First 学习笔记:约定配置 Entity Framework 复杂类型 Entity Framework 数据生成选项 ...

  6. EF对于已有数据库的Code First支持

    EF对于已有数据库的Code First支持 原文链接 本文将逐步介绍怎样用Code First的方式基于已有数据库进行开发.Code First支持你使用C#或者VB.Net定义类.并使用数据模型标 ...

  7. EF Code First学习笔记:数据库创建

    控制数据库的位置 默认情况下,数据库是创建在localhost\SQLEXPRESS服务器上,并且默认的数据库名为命名空间+context类名,例如我们前面的BreakAway.BreakAwayCo ...

  8. EF Code First学习笔记:数据库创建(转)

    控制数据库的位置 默认情况下,数据库是创建在localhost\SQLEXPRESS服务器上,并且默认的数据库名为命名空间+context类名,例如我们前面的BreakAway.BreakAwayCo ...

  9. EF Code-First 学习之旅 数据库初始化 (二)

    Context类的基类构造函数有如下的参数 1.无参数 如果没有给基类构造函数添加参数,它会在local SQLEXPRESS server创建数据库,名为{Namespace}.{Context c ...

随机推荐

  1. java导出简单写法

    List<SaleOrderExportDTO> list = TransferUtil.transferList(queryList, SaleOrderExportDTO.class) ...

  2. [LintCode] 全排列

    递归实现: class Solution { public: /** * @param nums: A list of integers. * @return: A list of permutati ...

  3. git pull报错:There is no tracking information for the current branch

    报错: There is no tracking information for the current branch. Please specify which branch you want to ...

  4. WM_MOUSEWHEEL消息

    使用WM_MOUSEWHEEL       需要把CWnd设定为Focus  ON_WM_MOUSEWHEEL(   )     CWnd::OnMouseWheel         afx_msg  ...

  5. 11.css定义下拉菜单

    注意点: 1.设置a标签的width 和 height 的时候,直接设置是没用的,可以以这样两种方式设置 (1). display:block; (2). float:left; 2.设置下拉菜单,最 ...

  6. 1.java中Comparor与Comparable的问题

    1.Comparator中compare()与Comparable中compareTo()方法的区别 Treeset集合创建对象后, A:如果是空构造,即TreeSet<Student> ...

  7. travelsal all files in a dir using recursion shell

    #!/bin/bash function getdir(){ ` do dir_or_file=$"/"$element if [ -d $dir_or_file ] then g ...

  8. elastic search 查询语句

    部署了半个月,分析一下数据: 需要提前知道的是,tpot中,每天的数据存一个index,然后每个index里面有不同的type,每条请求一个document 共24万条请求: 查看整个集群所有数据 以 ...

  9. linux c编程:Posix信号量

    POSIX信号量接口,意在解决XSI信号量接口的几个不足之处: POSIX信号量接口相比于XSI信号量接口,允许更高性能的实现. POSIX信号量接口简单易用:没有信号量集,其中一些接口模仿了我们熟悉 ...

  10. sql server内存使用情况

    查看Sql Server 数据库的内存使用情况 转自:https://www.cnblogs.com/wanghao4023030/p/8299478.html    DBCC MemoryStatu ...