• EF


       public class Article
        {
           [Key]
           public int ID { get; set; }
           public string Title{get;set;}

           public int AuthorssID { get; set; }

           public Author Author { get; set; }
        }

        public class Author
        {
            [Key]
            public int ID{get;set;}

            [MaxLength(50),Required]
            public string Name { get; set; }

            public string Address { get; set; }

        }

    CodeFirst生成的数据表:

    约定是以类名加ID来为外键命名的,如果找不到类名加ID的属性,则会成一个这样的规则来做为外键名。

    同时也可以强制加ForeignKey来标识外键

       public class NewArticle//ForeignKey 用来指示外键名为AuthorssID
        {
            [Key]
            public int ID { get; set; }
            public string Title { get; set; }
           
            public int AuthorssID { get; set; }

            [ForeignKey("AuthorssID")]
            public Author Author { get; set; }
        }

    生成的表为:

    ===============================================================================

        public class Department
        {
            [Key]
            public int ID{get;set;}

            [MaxLength(50)]
            public string Name { get; set; }


            //public virtual List<Employee> EmployeeList { get; set; }
            public List<Employee> EmployeeList { get; set; }
        }

      public class Employee
        {
            [Key]
            public int ID { get; set; }

            [MaxLength(30)]
            public string Name { get; set; }
        }

    生成的数据表:可看到自动为Employee 添 加了一个Department_ID的外键

    添加一个virtual

        public class Department1
        {
            [Key]
            public int ID{get;set;}

            [MaxLength(50)]
            public string Name { get; set; }
            public virtual List<Employee1> EmployeeList { get; set; }
        }

        public class Employee1
        {
            [Key]
            public int ID { get; set; }

            [MaxLength(30)]
            public string Name { get; set; }
        }

    生成的数据表:

    ==================================

        public class Department3
        {
            [Key]
            public int ID{get;set;}

            [MaxLength(50)]
            public string Name { get; set; }

            public virtual List<Employee3> EmployeeList { get; set; }
        }

        public class Employee3
        {
            [Key]
            public int ID { get; set; }

            [MaxLength(30)]
            public string Name { get; set; }

            public int DepartmentID{get;set;}

            [ForeignKey("DepartmentID")]
            public virtual Department3 Department { get; set; }
        }

     

    综上看来,导航属性(Virtual)与生成表结构没有关系,只是用来标识在LazyLoading时才会有用得到。

    接下来,接着试验。

    /// <summary>
        /// 会员
        /// </summary>
        public class User 
        {
            /// <summary>
            /// 用户ID
            /// </summary>
            [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
            public Int64 UserId { get; set; }
    
            /// <summary>
            /// 用户名
            /// </summary>
            [Required]
            [StringLength(50)]
            public string NickName { get; set; }
    
            /// <summary>
            /// 渠道负责人ID
            /// </summary>
            public int ChannelId { get; set; }
    
            /// <summary>
            /// 达人用户级别
            /// </summary>
            public int UserRank { get; set; }
    
            /// <summary>
            /// 渠道负责人
            /// </summary>
            public virtual Channel Channel { get; set; }
    
            [NotMapped]
            public Guid Id { get; set; }
        }
    /// <summary>
        /// 渠道
        /// </summary>
        public class Channel 
        {
            [Key]
            public int ChannelId { get; set; }
    
            /// <summary>
            /// 渠道名
            /// </summary>
            [StringLength(100)]
            [Required]
            public string ChannelName { get; set; }
    
            /// <summary>
            /// 是否可用
            /// </summary>
            public int IsActive { get; set; }
    
            [NotMapped]
            public Guid Id { get; set; }
        }

    运行测试

                    var vUser = db.User.FirstOrDefault(p => p.UserId > 0);
                    Console.WriteLine(vUser.NickName);
                    Console.WriteLine(vUser.Channel.ChannelName);///如果Channel不为Virtual则在此处会报错,除非用Include显示加载
  • 相关阅读:
    linux 硬盘满了 怎么优化
    Mysql 忘记密码 Linux
    嵌入式新闻早班车-第18期
    《安富莱嵌入式周报》第225期:2021.08.09--2021.08.15
    【DSP教程】第43章 IIR滤波器的Matlab设计
    【DSP教程】第42章 IIR无限冲击响应滤波器设计
    嵌入式新闻早班车-第17期
    《安富莱嵌入式周报》第224期:2021.08.02--2021.08.08
    H7-TOOL重大更新,发布WiFi版,新增暗黑主题,脱机烧录增加大唐半导体,自此高速USB,以太网和WiFi方式全部打通(2021-08-07)
    【STM32H7的DSP教程】第41章 FIR滤波器的群延迟(重要)
  • 原文地址:https://www.cnblogs.com/xuxu-dragon/p/3759410.html
Copyright © 2020-2023  润新知