英文:
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<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);
}
}
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
集合的用户。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论