0

我有以下课程

public pratial class Address
{
    public Guid AddressID{ get; set; }
    public AddressType AddressType{ get; set; }
}

public partial class AddressType
{
    public string TypeName{ get; set; }
}

在我派生的 DBContext 类中,我已经覆盖了 OnModelCreating

protected override OnModelCreating(DBModelBuilder modelBuilder)
{
    modelBuilder.Entity<Address>().HasKey( p => p.AddressID );
    modelBuilder.Entity<Address>().Property ( p => p.AddressID)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    modelBuilder.Entity<Address>().HasRequired( p => p.AddressType);
    modelBuilder.Entity<AddrssType>().HasKey( p => p.TypeName );

    ...
}

这很好,我在数据库中填写了一条记录

我在数据库中的表最终看起来像这样

Addresses Table
AddressID (PK, uniqueidentified, not null)
AddressType_TypeName(FK, nvarchar(32), not null)

AddressTypes Table
TypeName (PK, uniqueidentifies, not null)

现在我把一些数据放在表中

AddressTypes Record TypeName 我放在 Business

在我放入业务的地址记录 AddressType_TypeName

当我对此进行单元测试时,我希望能重新记录下来

List<Address> addresses = context.Addresses.ToList()
Assert.AreEqual(addresses[0].AddressType.TypeName, "Business");

但这无法告诉我 AddressType 为空

如何设置 Address 和 AddressType 之间的关系,以便我取回已连接的 AddressType?

4

1 回答 1

0

要加载相关实体,您必须告诉它 Entity Framework,或者使用急切加载

using System.Data.Entity;

//...

var addresses = context.Addresses.Include(a => a.AddressType).ToList()

...或者通过默认启用的延迟加载,如果您将导航属性标记为virtual

public virtual AddressType AddressType { get; set; }

急切加载将父数据和相关数据一起加载到单个数据库往返中。延迟加载需要两次往返,第二次发生在 convers 下,当您访问该行中的导航属性时addresses[0].AddressType.TypeName

编辑

测试项目以显示此示例中的延迟加载有效(EF 5.0、.NET 4.0、SQL Server Express 2008 R2 作为数据库)。我只放在virtual前面AddressType。其余的与您的模型相同:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;

namespace EFLazyLoading
{
    public partial class Address
    {
        public Guid AddressID{ get; set; }
        public virtual AddressType AddressType{ get; set; }
    }

    public partial class AddressType
    {
        public string TypeName{ get; set; }
    }

    public class MyContext : DbContext
    {
        public DbSet<Address> Addresses { get; set; }
        public DbSet<AddressType> AddressTypes { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Address>().HasKey( p => p.AddressID );
            modelBuilder.Entity<Address>().Property ( p => p.AddressID)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<Address>().HasRequired( p => p.AddressType);
            modelBuilder.Entity<AddressType>().HasKey( p => p.TypeName );
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>());
            using (var ctx = new MyContext())
            {
                var address = new Address
                {
                    AddressType = new AddressType { TypeName = "Business" }
                };
                ctx.Addresses.Add(address);
                ctx.SaveChanges();
            }

            using (var ctx = new MyContext())
            {
                List<Address> addresses = ctx.Addresses.ToList();

                string typeName = addresses[0].AddressType.TypeName;
            }
        }
    }
}

最后一行的结果typeName符合预期:

延迟加载

于 2013-04-06T13:35:16.427 回答