sách gpt4 ai đã đi

mariadb - Entity Framework Core + MariaDB - 导航属性为空

In lại 作者:行者123 更新时间:2023-12-04 02:13:34 25 4
mua khóa gpt4 Nike

我有一个使用 MariaDB 中的表定义的多对多关系。我正在尝试将 Entity Framework Core 中的导航属性用于相关实体,但它们没有水合。
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().Include("property_name") 的模式来补充导航属性。通过对 T 进行反射的扩展方法并查找所有具有属性 [Include] 的属性在他们身上定义。

但是,我无法让它正常工作。导航属性 RoleUserGroup返回空值。

你认为我做错了什么?如果您需要在问题中添加更多详细信息,也请告诉我!!

1 Câu trả lời

问题可能是 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 UserGroups { get; set; }
    public DbSet Roles { get; set; }
    public DbSet 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
    }

    延迟加载要求类型是公共(public)的,要延迟加载的属性必须是虚拟的:
    [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 使用手动急切或显式加载。 .

    关于mariadb - Entity Framework Core + MariaDB - 导航属性为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56124947/

    25 4 0
    行者123
    Hồ sơ cá nhân

    Tôi là một lập trình viên xuất sắc, rất giỏi!

    Nhận phiếu giảm giá Didi Taxi miễn phí
    Mã giảm giá Didi Taxi
    Giấy chứng nhận ICP Bắc Kinh số 000000
    Hợp tác quảng cáo: 1813099741@qq.com 6ren.com