zoukankan      html  css  js  c++  java
  • 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显示加载
  • 相关阅读:
    UI:UITableView表视图
    UI:页面传值、单例模式传值、属性传值、NSUserDefaults 数据持久化
    UI:UINavigationController、界面通信
    UI:UIScrollView、UIPageControl
    UI:tomcat(说话小程序)、相框动画、UISgmentcontrol、UISwitch
    UI:触摸事件 与 事件的回应
    UI:转自互联网资料
    UI:MVC设计模式
    OC:copy 与 retain 的区别
    UI:数据持久化
  • 原文地址:https://www.cnblogs.com/xuxu-dragon/p/3759410.html
Copyright © 2011-2022 走看看