当前位置: 首页 > news >正文

EF Code-First 学习之旅 Code First Conventions

协定是一系列的默认规则用来自动配置领域中的概念模型

1:类型发现

  Code-First对包含DBSet属性的类型创建表(包括这些类型的所有引用类型)

  

public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public byte[]  Photo { get; set; }
    public decimal Height { get; set; }
    public float Weight { get; set; }
        
    public Teacher Teacher { get; set; }

    public Standard Standard { get; set; }
}

public class Teacher
{
    public Teacher()
    { 
        
    }
    public int TeacherId { get; set; }
    public string TeacherName { get; set; }
}
    
namespace EF_Code_First_Tutorials
{
        
    public class SchoolContext: DbContext 
    {
        public SchoolContext(): base()
        {
            
        }
            
        public DbSet<Student> Students { get; set; }
        public DbSet<Standard> Standards { get; set; }
            
    }
}

 

 上图所示:即使上下文不包含Teacher的实体集,Code-First也为Teacher创建表

Code-First发现类型的协定:

1)上下文中的DBSet属性类型

2)引用的类型,即使在不同程序集中

3)DBSet属性的子类

 

主键约定

默认是:属性名是Id或者是类名+id的属性作为主键

主键的类型可以是任何类型,如果主键的数据类型为数字或者是GUID,则被配置为标识列

如果定义主键为其他名字,则会抛出ModelValidationException 异常

public class Standard
{
    public Standard()
    { 
        
    }
    public int StdId { get; set; }
    public string StandardName { get; set; }
    
    public IList<Student> Students { get; set; }
   
    }

 

 Standard定义StdId为主键,EntityFramework会抛出异常

'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll
EntityType 'Standard' has no key defined. Define the key for this EntityType.

 

如果要定义StdId为主键,则需要DataAnnotations or Fluent API来配置主键

 


关系约定

 

Code First推断两个实体之间的关系用到导航属性

导航属性可以是简单的引用类型或集合

例如在Student中的导航属性为Standard,Standard中的导航属性为ICollection<Student>

因此,Code First自动为Standard和Student建立一对多的关系

在Student表中建立名为Standard_StandardId 的外键

public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public byte[]  Photo { get; set; }
    public decimal Height { get; set; }
    public float Weight { get; set; }
        
    //Navigation property
    public Standard Standard { get; set; }
}

public class Standard
{
    public Standard()
    { 
        
    }
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    //Collection navigation property
    public IList<Student> Students { get; set; }
   
}

因此,Code First默认约定外键的名字为:<导航属性名>_<导航属性的主键>

 

外键约定

 

public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public byte[]  Photo { get; set; }
    public decimal Height { get; set; }
    public float Weight { get; set; }
        
    //Foreign key for Standard
    public int StandardId { get; set; }

    public Standard Standard { get; set; }
}

public class Standard
{
    public Standard()
    { 
        
    }
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public IList<Student> Students { get; set; }
   
    }

 

上面的例子中,Student类包含外键StandardId(Standard类的主键),Code First将在Students表中创建StandardId列名代替Standard_StandardId列

 

 上面的外键不可为null,因为int类型不是nullable

如果定义为nullable,则可为null

 

复杂类型约定

 

Code-First默认的约定

 

Default Convention ForDescription
Table Name<实体类名> + 's' 
EF will create DB table with entity class name suffixed by 's'
Primary key Name1) Id 
2) <实体类名> + "Id" (忽略大小写) 

EF will create primary key column for the property named Id or <Entity Class Name> + "Id" (case insensitive)
Foreign key property NameBy default EF will look for foreign key property with the same name as principal entity primary key name. 
If foreign key property does not exists then EF will create FK column in Db table with <Dependent Navigation Property Name> + "_" + <Principal Entity Primary Key Property Name> 
e.g. EF will create Standard_StandardId foreign key column into Students table if Student entity does not contain foreignkey property for Standard where Standard contains StandardId
Null column所有的引用类型列为null,基类型属性为nullable
Not Null Column主键属性为notnull,non-nullable
DB Columns order表中的列排序与实体中的属性排序是一样的,不过主键会被移到首位
Properties mapping to DB所有的属性都被映射到列中,使用[NotMapped]特性排除属性或列不进行映射
Cascade deleteEnabled By default for all types of relationships.

 

 C#中的数据类型与表中的数据类型对应关系

 

C# DataTypeRelated DB Column DataTypePK Column DataType & Length
intintint, Identity column increment by 1
stringnvarchar(Max)nvarchar(128)
decimaldecimal(18,2)decimal(18,2)
floatrealreal
byte[]varbinary(Max)varbinary(128)
datetimedatetimedatetime
boolbitbit
bytetinyinttinyint
shortsmallintsmallint
longbigintbigint
doublefloatfloat
charNo mappingNo mapping
sbyteNo mapping 
(throws exception)
No mapping
objectNo mappingNo mapping

 

转载于:https://www.cnblogs.com/lanpingwang/p/6624462.html

相关文章:

  • Excel从零整理
  • 线段树建图
  • C#编程(七十六)----------使用指针实现基于栈的高性能数组
  • CSS-样式表的分类以及选择器的分类
  • childNodes与children
  • 发现一个很N且免费的html5拓扑图 关系图 生成组件
  • I2S
  • Oracle11g表空间导入dmp数据
  • Ambari里如何删除某指定的服务(图文详解)
  • CP-ABE ToolKit 安装笔记
  • js数组去重的三种常用方法总结
  • DPDK QoS之分层调度器
  • 对于文本框的验证(考虑兼容问题)
  • 114. Flatten Binary Tree to Linked List (leetcode)
  • 大数据与应用统计学的区别与联系
  • 《Javascript高级程序设计 (第三版)》第五章 引用类型
  • 5分钟即可掌握的前端高效利器:JavaScript 策略模式
  • echarts的各种常用效果展示
  • iOS 系统授权开发
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • Redis的resp协议
  • redis学习笔记(三):列表、集合、有序集合
  • Selenium实战教程系列(二)---元素定位
  • 成为一名优秀的Developer的书单
  • 官方新出的 Kotlin 扩展库 KTX,到底帮你干了什么?
  • 后端_ThinkPHP5
  • 简单数学运算程序(不定期更新)
  • 聊聊flink的TableFactory
  • 微信小程序上拉加载:onReachBottom详解+设置触发距离
  • 新版博客前端前瞻
  • ​​​​​​​​​​​​​​Γ函数
  • ​决定德拉瓦州地区版图的关键历史事件
  • ​软考-高级-信息系统项目管理师教程 第四版【第23章-组织通用管理-思维导图】​
  • #每日一题合集#牛客JZ23-JZ33
  • $.each()与$(selector).each()
  • (2)(2.10) LTM telemetry
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (NO.00004)iOS实现打砖块游戏(十二):伸缩自如,我是如意金箍棒(上)!
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (三分钟)速览传统边缘检测算子
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • (转)程序员技术练级攻略
  • (轉貼) 2008 Altera 亞洲創新大賽 台灣學生成果傲視全球 [照片花絮] (SOC) (News)
  • (轉貼) 蒼井そら挑戰筋肉擂台 (Misc)
  • (自适应手机端)响应式新闻博客知识类pbootcms网站模板 自媒体运营博客网站源码下载
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .bat批处理(四):路径相关%cd%和%~dp0的区别
  • .CSS-hover 的解释
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .NET 自定义中间件 判断是否存在 AllowAnonymousAttribute 特性 来判断是否需要身份验证
  • .NET/ASP.NETMVC 深入剖析 Model元数据、HtmlHelper、自定义模板、模板的装饰者模式(二)...
  • .NET/C# 异常处理:写一个空的 try 块代码,而把重要代码写到 finally 中(Constrained Execution Regions)
  • .NET中GET与SET的用法
  • /etc/skel 目录作用