zoukankan      html  css  js  c++  java
  • EF Codefirst(二)数据注释

    CodeFirst通过分析我们在代码里编写的类,以及类之间的关系生成数据库表,以及表之间的各种关系。数据库的表会涉及到主键,外键,列是否为空,列类型等等。 我们要通过怎样的方式来暴露这些信息呢?
     
    CodeFirst通过DataAnnotations(在 System.ComponentModel.DataAnnotations 命名空间中 )特性类标示这些信息。

    常用的一些标识如下 

    主键

        如果不存在符合EF默认规则的主键时,用key标注也可。个人不赞成这样,遵守默认规则就好,免去不必要麻烦。
        public class Category
        {
            [ Key ]
            public string TrackingKey { get; set; }
            public string CategoryName { get; set; }
            public string BlogID { get; set; }
        }

    自增长

    如果主键是int类型,EF为默认设置为增长。但如果是GUID类型,则要手动的设置自增长
       public class User
        {
            [Key ,DatabaseGenerated (DatabaseGeneratedOption .Identity)]
            public Guid UserGuid { get; set; }
            public string UserName { get; set; }
        }
    查看数据表的创建脚本,我们会发现多了这样的一句话
    ALTER TABLE [dbo]. [Users] ADD   DEFAULT ( newsequentialid ()) FOR[UserGuid]

    必需 MaxLength 和 MinLength

        public class Category
        {
            [ Key ]
            public string TrackingKey { get; set; }
            [ Required ][MaxLength (30), MinLength(5)]
            public string CategoryName { get; set; }
            public string BlogID { get; set; }      
            public string Description { get; set; }
        }

    NotMapped

    Code First 约定指示具有受支持数据类型的每个属性都要在数据库中有表示。但在您的应用程序中并不总是如此。例如,您可以在 Blog 类中使用一个属性来基于 Title 和 BloggerName 字段创建代码。该属性可以动态创建,无需存储。您可以使用 NotMapped 注释来标记不映射到数据库的所有属性。
    并且这个标示还可以用于对整个类进行标示,那这个类也就不会被映射了。
     

    ComplexType

     BlogDetails 作为 Blog 类中的一个属性,将作为 Blog 对象的一部分被跟踪。为了让 Code First 认识到这一点,必须将 BlogDetails 类标记为 ComplexType。
     
         public class Blog
        {
            public string ID { get; set; }
            public string BlogName { get; set; }
            public string BlogAuthor { get; set; }
            public virtual List < Post> Posts { get ; set ; }      //导航属性        
            public BlogDetails Detail { get; set; }
        }
    
        [ ComplexType]
        public class BlogDetails
        {
            public string BlogDescription { get; set; }
            public string CreateTime { get; set; }
        }

    但是运行的时候却报错,提示Detail属性,不可以为空 。原来,虽然复杂类型的每个属性都可以为空,但当作为复杂属性出现时,却是不可以为空的,必须初始化!
    需要修改如下
    修改之后运行如下
    会发现 复杂类型将自己的属性并入了所属类型,并以复杂类型名+属性名作为新列名
     
    数据注释就到这里。喜欢欢迎推荐!
     
  • 相关阅读:
    深入Android 【一】 —— 序及开篇
    Android中ContentProvider和ContentResolver使用入门
    深入Android 【六】 —— 界面构造
    The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the ser
    Dynamic Business代码片段总结
    对文件的BuildAction以content,resource两种方式的读取
    paraview 3.12.0 windows下编译成功 小记
    百度网盘PanDownload使用Aria2满速下载
    netdata的安装与使用
    用PS给证件照排版教程
  • 原文地址:https://www.cnblogs.com/JasonShenW/p/5084363.html
Copyright © 2011-2022 走看看