zoukankan      html  css  js  c++  java
  • EntityFramework 学习 一 实体类型

    我们为已存在的数据库创建EDM,EDM包含与数据库中表对应的实体。EF中有两种实体类型 POCO entity dynamic proxy entity

    POCO Entity (Plain Old CLR Object):

    POCO类是不依赖任何框架的类,它就想.net中普通的类,称作“Plain Old CLR Objects”

    POCO实体类支持很多查询、添加、更新和删除行为

    POCO例子

    public class Student
    {
        public Student()
        {
            this.Courses = new List<Course>();
        }
        
        public int StudentID { get; set; }
        public string StudentName { get; set; }
        public Nullable<int> StandardId { get; set; }
        
        public Standard Standard { get; set; }
        public StudentAddress StudentAddress { get; set; }
        public IList<Course> Courses { get; set; }
    }

    Dynamic Proxy (POCO Proxy):

    Dynamic Proxy是运行时POCO的代理类,它就像是POCO的包装类,Dynamic Proxy允许延迟加载和自动变化跟踪

    POCO满足以下条件才能成为Dynamic Proxy类

    1.POCO必须是public访问

    2.POCO不是密封类

    3.POCO不是抽象类

    4.每个导航属性必须声明为Public和virtual

    5.每一个集合属性必须是ICollection<T>

    6.ProxyCreationEnabled 选项必须是true

    public class Student
            {
                public Student()
                {
                    this.Courses = new HashSet<Course>();
                }
        
                public int StudentID { get; set; }
                public string StudentName { get; set; }
                public Nullable<int> StandardId { get; set; }
        
                public virtual Standard Standard { get; set; }
                public virtual StudentAddress StudentAddress { get; set; }
                public virtual ICollection<Course> Courses { get; set; }
            }

    注意:默认的,Dynamic Proxy对每个实体都是可用的,然而,可用在上下文中通过设置ProxyCreationEnabled 选项不可用

    context.Configuration.ProxyCreationEnabled = false;

    在运行当中,Student类型将是 System.Data.Entity.DynamicProxies.Student

    从Dynamic Proxy中获取实体的真正类型

    可用使用ObjectContext.GetObjectType()方法找到真正的实体类型

     实体可用有两种类型属性 Scalar 和Navigation

     Scalar属性是实体中真实的值,例如,Student实体中的Scalar属性 象StudentId和StudentName,这符合Student表的列

    导航属性:导航属性指向另一个关联的实体,Student有Standard属性作为导航属性

  • 相关阅读:
    DTD与shema学习
    xml基本语法学习
    快速写出main方法和system.out.print()
    Eclipse常见快捷键
    System.out.print()与toString()
    HttpURLConnection学习
    如何查看开关机时间
    阿里云云服务器硬盘分区及挂载
    java环境搭建
    使用jstack 发现死锁
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6596865.html
Copyright © 2011-2022 走看看