3.14.- 创建POCO代理
如果想要启用POCO实体的延迟加载和实体框架跟踪类的变化,你的POCO类必须满足该主题的需求,这样
实体框架就可以在运行时创建POCO实体的代码。代理类从你的POCO类型中派生、
3.14.1.- POCO实体类定义的需求
如果类满足下面描述的实体框架会创建POCO实体的代理。POCO实体可以有支持改变跟踪或延迟加载的代理对象。
你可以不满足改变跟踪而具有延迟加载的代理,但如果你满足了改变跟踪,延迟加载代理也会被创建。你可以
通过设置LazyLoadingEnabled的选项为false来禁止延迟加载。
对于要创建的代理:
• 类必须声明为public
• 类必须不是sealed
• 类必须不是abstract
• 类必须有一个public或protected的没有参数的构造器
• ProxyCreationEnabled选项必须设置为true
对延迟加载代理:
• 每个导航属性必须有non-sealed, public,和virtual的get访问器
// Example of a Navigation Property ready for Proxy
public class BankAccount : Entity
{
//... Ommitted ...
public virtual ICollection<BankTransferLog> TransfersHistory { get; set; }
//... Ommitted ...
}
对改变跟踪代理:
• 每个属性必须有non-sealed, public,和virtual的get和set访问器
• 代表“多”端的导航属性必须返回实现System.Collections.Generic.ICollection of T的类型,
其中T是关系另一端的对象类型。
• 如果你想让代理类型和你的对象一起创建,那么使用System.Data.Entity.DbSet.Create方法而不是new创建对象。
3.15.- 代码优先中的复杂类型
复杂类型在EF 4.1中很容易实现。想象客户实体类有一些像城市,邮政编码和街道的属性,我们发现把这些属性
组织成一个叫地址的复杂类型会比较好。
//Initial Customer Entity
//
public class Customer : Entity
{
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public string Street { get; set; }
public string ZipCode { get; set; }
}
Figure 22.- Initial plain Customer entity
//Initial Customer Entity
//
public class Customer : Entity
{
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string City { get; set; }
public string Street { get; set; }
public string ZipCode { get; set; }
}
考虑到我们不需要做其他任何事,EF转换会处理好复杂类型和映射到数据库中。默认转换会是
[复杂类型名]_[属性名]。因此,SQL schema会是这样的。
CREATE TABLE [dbo].[Customers](
[CustomerId] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](128) NULL,
[LastName] [nvarchar](128) NULL,
[Address_City] [nvarchar](128) NULL,
[Address_ZipCode] [nvarchar](128) NULL,
[Address_Street] [nvarchar](128) NULL,
PRIMARY KEY CLUSTERED
(
[CustomerId] ASC
Address complex type
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
) ON [PRIMARY]
GO
我们可以修改这样的映射。如果我们用“Fluent API‟的方法,我们会使用DbModelBuilder类的ComplexType
方法,就像下面的代码。
//Using ‘ComplexType()’ method Fluent API – (Code in Data Persistence Infr.Layer)
//
protected override void OnModelCreating(DbModelBuilder modelBulder)
{
modelBuilder.Entity<Customer>()
.Ignore(c => c.FullName);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.ComplexType<Address>()
.Property(p => p.City)
.HasColumnName("City");
modelBuilder.ComplexType<Address>()
.Property(p => p.Street)
.HasColumnName("Street");
modelBuilder.ComplexType<Address>()
.Property(p => p.ZipCode)
.HasColumnName("ZipCode");
}
注意:
ComplexType<>提供和Entity<>一样的配置。
我们也可以把自定义映射的关系放到新派生于ComplexTypeConfiguration<TComplexType>的类中。
class AddressComplexTypeConfiguration
:ComplexTypeConfiguration<Address>
{
public AddressComplexTypeConfiguration()
{
this.Property(p => p.ZipCode)
.HasColumnName("ZipCode");
Using the ComplexType method (Fluent API)
NOTE:
ComplexType<> does offer the same configuration possibilities than Entity<>.
this.Property(p => p.Street)
.HasColumnName("Street");
this.Property(p => p.City)
.HasColumnName("City");
}
}
然后我们需要将它添加到OnModelCreating()方法的配置列表。这是复杂项目的推荐做法,这样我们
可以更好的构造它们。
public class MainBCUnitOfWork : DbContext
{
public IDbSet<Customer> Customers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new AddressComplexTypeConfiguration());
}
}
这段映射代理应该位于数据持久化层中。我们会在那章中详细解释。