实体框架核心+ MariaDB-导航属性为null

时间:2019-05-14 07:19:19

标签: mariadb entity-framework-core-2.2

我有一个使用MariaDB中的表定义的多对多关系。我正在尝试对相关实体使用Entity Framework Core中的Navigation Properties,但是它们没有水合。

roles

| role_id | role_name |
| 1       |  Role-A   |
| 2       |  Role-B   |

groups

| group_id |  group_name |
|  1       |   Group-A   |
|  2       |   Group-B   |

role_to_group

|  role_to_group_id   |   role_id  | group_id |
|          1          |    1       |   1      |
|          2          |    2       |   1      |
|          3          |    2       |   2      |

这是课程

[Table(name: "role_to_group")]
public class RoleToGroup
{
    [Column(name: "role_to_group_id")]
    public long RoleToGroupId { get; set; }

    [Column(name: "role_id")]
    [ForeignKey("RGToRoles")]
    public int RoleId { get; set; }

    [Column(name: "group_id")]
    public int UserGroupId { get; set; }

    [Include]
    public Role Role { get; set; }
    [Include]
    public UserGroup UserGroup { get; set; }
}

[Include]是一个自定义定义的属性,用于通过类似于DbContext.Set<T>().Include("property_name")的扩展方法对导航属性进行水合,该扩展方法会在T上进行反射,并找到具有属性{ {1}}在它们上定义。

但是,我无法使其正常工作。导航属性[Include]Role返回null。

您认为我做错了什么?另外,如果您需要其他更多信息,请告诉我!

1 个答案:

答案 0 :(得分:0)

问题可能是EF Core不会自动执行延迟加载。 (Documentation about how to implement lazy loading)。

在安装以下NuGet软件包时:

  • Microsoft.EntityFrameworkCore.Proxies
  • Microsoft.EntityFrameworkCore.Relational

并启用了延迟加载,例如:

    public class MyDbContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySQL(@"User Id=root;Host=localhost;Database=Test;");

            optionsBuilder
                .UseLazyLoadingProxies();
        }

        public DbSet<UserGroup> UserGroups { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<RoleToGroup> RoleToGroups { get; set; }
    }

我把它装好了

        using (var context = new MyDbContext())
        {
            var roleGroup1 = context.RoleToGroups.First();
            Console.WriteLine(roleGroup1.Role.Name); //Role-A
            Console.WriteLine(roleGroup1.UserGroup.Name); //Group-A
        }

延迟加载要求类型是公共的,并且延迟加载的属性必须是虚拟的:

[Table(name: "role_to_group")]
public class RoleToGroup
{
    [Column(name: "role_to_group_id")]
    public int RoleToGroupId { get; set; }

    [Column(name: "role_id")]
    [ForeignKey("roles")]
    public int RoleId { get; set; }

    [Column(name: "group_id")]
    [ForeignKey("groups")]
    public int UserGroupId { get; set; }

    public virtual Role Role { get; set; } //must be virtual
    public virtual UserGroup UserGroup { get; set; }//must be virtual
}

[Table(name: "roles")]
public class Role
{
    [Key]
    [Column(name: "role_id")]
    public int RoleId { get; set; }

    [Column(name: "role_name")]
    public string Name { get; set; }
}

[Table(name: "groups")]
public class UserGroup
{
    [Key]
    [Column(name: "group_id")]
    public int GroupId { get; set; }

    [Column(name: "group_name")]
    public string Name { get; set; }
}

除了延迟加载外,还可以从同一doc使用手动Eager或显式加载。