Entity Framework 6.0,Identity,IdentityUser类中的一对多关系不起作用

huangapple go评论91阅读模式
英文:

Entity Framework 6.0, Identity, one-to-many relationship not working in IdentityUser class

问题

我正在为与Identity和Entity Framework相关的问题感到困惑。我正在使用代码优先方法。

我有以下类:

public class WebApplicationUser : IdentityUser
{
    public virtual ICollection<Favorite> Favorites { get; set; }
}

public class Favorite
{
    public int Id { get; set; }
    public string? ImageName { get; set; }

    public virtual WebApplicationUser WebApplicationUser { get; set; }
}

public class WebApplicationContext : IdentityDbContext<WebApplicationUser>
{
    public WebApplicationContext(DbContextOptions<WebApplicationContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }
}

我想添加如上所示的Favorites属性。

当我使用代码添加新的Favorite时,一切都正常,并且它会传输到数据库中。

我通过获取当前用户对象来实现这一点:

var user = await _userManager.GetUserAsync(HttpContext.User);
user.Favorites.Add

问题在于,由于某种原因,数据库中现有的收藏夹不会填充到user.Favorites集合中(因此它们不会被读取回来)。

当我向集合中添加元素时,它们会写入数据库,但当我尝试在另一个控制器操作中稍后读取收藏夹时,我始终会获得一个空集合。

有人知道出了什么问题吗?

我在其他论坛中尝试了不同的解决方案,但什么都不起作用。

英文:

I am getting crazy with a problem related to Identity and Entity Framework. I am using a code-first approach.

I have these classes:

public class WebApplicationUser : IdentityUser
{
    public virtual ICollection&lt;Favorite&gt; Favorites { get; set; }
}

public class Favorite
{
    public int Id { get; set; }
    public string? ImageName { get; set; }

    public virtual WebApplicationUser WebApplicationUser { get; set; }
}

public class WebApplicationContext : IdentityDbContext&lt;WebApplicationUser&gt;
{
    public WebApplicationContext(DbContextOptions&lt;WebApplicationContext&gt; options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }
}

I want to add the Favorites property as shown in the code above.

When I add a new Favorite using code, it's working fine and it's getting transferred to the database.

I do that by getting the current user object

var user = await _userManager.GetUserAsync(HttpContext.User);
user.Favorites.Add

The problem is that for some reason the existing favorites from the database are not filled into my user.Favorites collection (so they are not read back).

When I add elements to the collection they are written to the db but when I try to read the favorites later in another controller action I always get an empty collection.

Does someone have an idea what's going wrong?

I tried different solutions in other forums but nothing is working

答案1

得分: 0

你需要使用.Include(xx)在EF Core中加载相关数据,因此在这里你可以使用:

var result = await _userManager.Users.Include(x => x.Favorites).Where(x => x.Id == HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)).FirstOrDefaultAsync();

来加载带有user.Favorites集合的用户。

英文:

You need to use .Include(xx) to load related data in EF Core, So here you can use:

var result  = await _userManager.Users.Include(x=&gt;x.Favorites).Where(x=&gt;x.Id== HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)).FirstOrDefaultAsync();

to load user with user.Favorites collection.

Entity Framework 6.0,Identity,IdentityUser类中的一对多关系不起作用

huangapple
  • 本文由 发表于 2023年3月7日 04:29:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定