Entity Framework 4 第二篇 POCO 2

简介:

Entity Framework 4第二篇 POCO 2

本篇,继续介绍关于EF 4中的POCO有关数据定义(DDL)方面的东西,比如:指定主键,设置字段长度,指定浮点字段的精度,设置关系,约束,延迟加载等内容。相比EFV1来说,EF 4的确改进了不少。

0步,准备工作:

需要的环境:Windows 7+VS2010beta2+EF4CTP2+SQL SERVER2008

1步,建立数据模型类。例如,我们分别写了CompanyPerson两个类文件。为方便查看各种类型(如:string,decimal,byte[],bool,int,long,datetime)的字段最终映射到数据库表结构中情况,特意尽量使用了比较多的类型。Company实体类具体代码如下:

/// <summary>

/// 对应数据库的Company

/// </summary>

    public class Company

    {

        public long ID { getset; }

        public string Name { getset; }

        public string HomePage { getset; }

        public virtual ICollection<Person> Persons { getset; }

    }

CompanyPOCO类文件中,我们使用了virtual修饰符来修饰集合Persons,这样以便创建代理类以便延迟加载时使用。下面是Person实体类代码:

/// <summary>

    /// 对应数据库中的Person

    /// </summary>

    public class Person

    {

        public int ID { getset; }

        public string Name { getset; }

        public string Position { getset; }

        public Company PersonCompany { getset; }

        public DateTime BirthDay { getset; }

        public byte[] Photo { getset; }

        public Decimal Salary { getset; }

        public string Address { getset; }

        public bool IsMale { getset; }

    }

2步,设置字段属性和关系等。EF4中提供了EntityConfiguration<TEntity>这个类,我们就是通过它用来指定各种实体类的字段属性和关系等。因此,我们需要自己编写继承EntityConfiguration的实体类配置文件。代码如下:

/// <summary>

    /// Company的配置类

    /// </summary>

    public class CompanyCfg : EntityConfiguration<Company>

    {

        public CompanyCfg()

        {

            Property(c => c.ID).IsIdentity();

            Property(c => c.Name).HasMaxLength(32).IsRequired();

            Property(c => c.HomePage).IsOptional().HasMaxLength(128);

            //1..*的关系

            Relationship(c => c.Persons).IsOptional();

            //设置反向,关系有子对象Person来维护

            Relationship(c => c.Persons).FromProperty(p => p.PersonCompany);

        }

    }

 

/// <summary>

    /// Person的配置类

    /// </summary>

    public class PersonCfg : EntityConfiguration<Person>

    {

        public PersonCfg()

        {

            Property(p => p.ID).IsIdentity();

            Property(p => p.Name).HasMaxLength(32).IsRequired();

            Property(p => p.Position).HasMaxLength(32);

            Property(p => p.Salary).HasPrecision(102);

            Property(p => p.Address).HasMaxLength(128);

            Property(p => p.Photo).IsMax();

            Relationship(p => p.PersonCompany).IsRequired();

            Relationship(p => p.PersonCompany).FromProperty(c => c.Persons);

        }

    }

在代码中,各个方法说明如下:

l   IsIdentity()表示是主键标识;

l   HasMaxLength()用来指定字段长度;

l   IsRequired()用来指定字段比填(不能为空);

l   HasPrecision()用来指定浮点类型字段的数据位数和精度;

l   IsMax()则设置字段类最大长度为此类型的默认最大长度;

l   IsOptional()表示可选即可为NULL

l   Relationship()用来设置关系;

l   HasConstraint()用来设置约束

 

3步,运行代码。最后,我们需要使用ContextBuilder中的Configurations.Add方法,将上面配置添加到当前的上下文中。代码如下:

[TestMethod]

        public void TestMethod1()

        {

            //

            // TODO: Add test logic here

            //

            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;user id=sa;PassWord=11111111;Initial Catalog=NewNorthwind");

            var builder = new ContextBuilder<POCOContext>();

//Add配置类的顺序无关

                builder.Configurations.Add(new PersonCfg());

            builder.Configurations.Add(new CompanyCfg());

            POCOContext context = builder.Create(con);

            if (context.DatabaseExists())

                context.DeleteDatabase();

            context.CreateDatabase();

        }

最后,运行上面的,我们可以看到将在数据库建立了两张表。如下图所示:

 

仔细对比发现:最终的表结构和我最开始想的不一样呢(如:Person表的BirthDay字段不可为空,我开始认为没有在配置类设置的字段应该是默认可空)。

原来,实体类中属性类型是可以Nullable的,在创建表结构时如果没有指定IsRequired,则默认创建的表结构中是可为NULL。实体类中属性类型不能是Nullable的,在创建表结构时,如果没有指定IsRequired,则默认创建的表结构是不能为NULL的。如:Person中,改为public DateTime? BirthDay { get; set; }则创建的表结构中BirthDay可为NULL

个人觉得,通过这样自己写代码Code Only来实现对数据表进行定义,没有了EDMX文件。总觉得EDMX文件包含所有表的描述、定义、映射,手动修改起来很是不方便。这样相比之下,写代码实现看上去的确是清爽了不少。但是,如果表比较多,表间关系比较复杂的话,估计写代码也容易弄混了。

 


    本文转自风车车  博客园博客,原文链接:http://www.cnblogs.com/xray2005/archive/2010/01/08/1642372.html,如需转载请自行联系原作者




相关文章
|
XML 存储 数据库连接
Entity Framework学习笔记——edmx文件
上文简单介绍了一下Entity FrameWork,这里说一下EF的核心——edmx文件。 在VisualStudio中建立edmx文件(此例环境为VisualStudio2012)
Entity Framework学习笔记——edmx文件
|
存储 开发框架 .NET
Entity Framework基础01
Entity Framework基础01
180 0
Entity Framework基础01
|
SQL 数据库 C++
Entity Framework初体验
Entity Framework初体验
157 0
Entity Framework初体验
|
SQL 开发框架 Oracle
Entity Framework简介
Entity Framework简介
137 0
|
存储 开发框架 数据可视化
Entity Framework Core 简介
Entity Framework Core 简介
172 0
|
SQL .NET 数据库
Entity Framework Core 入门(2)
安装 EF Core 将 EF Core 添加到不同平台和常用 IDE 中的应用程序的所需步骤汇总。 分步入门教程 无需具备 Entity Framework Core 或任何特定 IDE 的原有知识,即可学习这些入门教程。
1109 0
|
数据库 数据库管理
[UWP小白日记-11]在UWP中使用Entity Framework Core(Entity Framework 7)操作SQLite数据库(一)
原文:[UWP小白日记-11]在UWP中使用Entity Framework Core(Entity Framework 7)操作SQLite数据库(一) 前言   本文中,您将创建一个通用应用程序(UWP),使用Entity Framework Core(Entity Framework 7)框架在SQLite数据库上执行基本的数据访问。
1639 0
|
SQL .NET 数据库
Entity Framework Core 2.0 入门
该文章比较基础, 不多说废话了, 直接切入正题. 该文分以下几点: 创建Model和数据库 使用Model与数据库交互 查询和保存关联数据 EF Core支持情况 EF Core的数据库Providers: 此外还即将支持CosmosDB和 Oracle.
1636 0

热门文章

最新文章