EntityFramework延迟加载不起作用

时间:2014-12-08 11:30:32

标签: c# entity-framework

我是EntityFramework 6的新手,并在POCO中继续存在问题。

以下是我的代码

Domain.cs

public abstract class BaseEntity
{
    [Key]
    public long Id { get; set; }
}

public class User : BaseEntity
{
    public long UserRoleId { get; set; }

    public string Name { get; set; }

    public virtual _EntityUserRole UserRole { get; set; }    
}

public class UserRole : BaseEntity
{
    public string Name { get; set; }

    public virtual ICollection<_EntityUser> Users { get; set; }

    public virtual ICollection<_EntityUserPermission> UserPermissions { get; set; }

    internal _EntityUserRole()
    {
        Users = new List<_EntityUser>();
        UserPermissions = new List<_EntityUserPermission>();
    }
}

public class UserPermission : BaseEntity
{
    public string Name { get; set; }

    public virtual ICollection<_EntityUserRole> UserRoles { get; set; }

    internal _EntityUserPermission()
    {
        UserRoles = new List<_EntityUserRole>();
    }
}

Mapping.cs

public abstract class BaseEntityMap<T> : EntityTypeConfiguration<T> where T : BaseEntity
{
    public BaseEntityMap()
    {
        ToTable(typeof(T).Name);
        HasKey(a => a.Id);
    }
}

public class UserMap : BaseEntityMap<User>
{
    public UserMap()
        : base()
    {
        HasRequired(x => x.UserRole)
            .WithMany(x => x.Users)
            .HasForeignKey(x => x.UserRoleId);
    }
}

public class UserRoleMap : BaseEntityMap<UserRole>
{
    public UserRoleMap()
        : base()
    {
        HasMany(x => x.UserPermissions)
            .WithMany(x => x.UserRoles)
            .Map(x =>
            {
                x.MapLeftKey("UserRoleId");
                x.MapRightKey("UserPermissionId");
                x.ToTable("_EntityUserRole_UserPermission");
            });
    }
}

public class UserPermissionMap : BaseEntityMap<UserPermission>
{
    public UserPermissionMap()
        : base()
    {
    }
}

SqlServerDataContext.cs

internal class SqlServerDataContext : DbContext
{
    public DbSet<User> Users { get; set; }

    public DbSet<UserRole> UserRoles { get; set; }

    public DbSet<UserPermission> UserPermissions { get; set; }

    public SqlServerDataContext()
        : base("Name=MyDB")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new UserMap());
        modelBuilder.Configurations.Add(new UserRoleMap());
        modelBuilder.Configurations.Add(new UserPermissionMap());

        base.OnModelCreating(modelBuilder);
    }
}

Program.cs的

    private static void Main()
    {
        using (SqlServerDataContext ctx = new SqlServerDataContext())
        {
            UserRole role = new UserRole();
            role.Name = "Super";

            UserPermission prm1 = new UserPermission();
            prm1.Name = "Delete";
            role.UserPermissions.Add(prm1);

            UserPermission prm2 = new UserPermission();
            prm2.Name = "Add";
            role.UserPermissions.Add(prm2);

            UserPermission prm3 = new UserPermission();
            prm3.Name = "Edit";
            role.UserPermissions.Add(prm3);

            User user = new User();
            user.Name = "User 1";
            user.UserRole = role;

            ctx.Users.Add(user);
            ctx.SaveChanges();
        }

        using (SqlServerDataContext ctx = new SqlServerDataContext())
        {
            var b = ctx.Users.Find(1);
        }
    }

结果 user retrieve

在打印屏幕中,我将记录插入用户后,当我检索回来时, UserPermissions 为空。请帮忙!感谢。

1 个答案:

答案 0 :(得分:0)

将模型添加到集合后,您需要SaveChanges(),以便将它们持久保存到数据库中。

然后您可以从数据库中读取您需要的内容。

之后,您需要访问该属性或使用Include(使其更加渴望,这是首选用途)。

private static void Main()
{
    using (SqlServerDataContext ctx = new SqlServerDataContext())
    {
        UserRole role = new UserRole();
        role.Name = "Super";

        UserPermission prm1 = new UserPermission();
        prm1.Name = "Delete";
        role.UserPermissions.Add(prm1);

        UserPermission prm2 = new UserPermission();
        prm2.Name = "Add";
        role.UserPermissions.Add(prm2);

        UserPermission prm3 = new UserPermission();
        prm3.Name = "Edit";
        role.UserPermissions.Add(prm3);

        ctx.SaveChanges();

        User user = new User();
        user.Name = "User 1";
        user.UserRole = role;

        ctx.Users.Add(user);

        ctx.SaveChanges();
    }

    using (SqlServerDataContext ctx = new SqlServerDataContext())
    {
        var b = ctx.Users.Find(1);

        // Method 1
        var roleName = b.UserRole.Name;

        // Method 2
        ctx.Users.Include(x => x.UserRole).Include(x => x.UserRole.UserPermissions).FirstOrDefault(x => x.Id == 1);
    }
}