Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 标识用户对象未映射到另一个对象-实体框架_C#_Sql Server_Entity Framework_.net Core_Asp.net Identity - Fatal编程技术网

C# 标识用户对象未映射到另一个对象-实体框架

C# 标识用户对象未映射到另一个对象-实体框架,c#,sql-server,entity-framework,.net-core,asp.net-identity,C#,Sql Server,Entity Framework,.net Core,Asp.net Identity,因此,我使用ASP.Identity和EntityFramework制作了.NET核心应用程序,采用代码优先的方法。 我的AppUser类: public class AppUser : IdentityUser<string> { public string Name { get; set; } public string Surname { get; set; } public DateTime DateJoined { get; set; } } 还有

因此,我使用ASP.Identity和EntityFramework制作了.NET核心应用程序,采用代码优先的方法。 我的AppUser类:

public class AppUser : IdentityUser<string>
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public DateTime DateJoined { get; set; }
}
还有我的应用程序Bcontext

 public class ApplicationDbContext : IdentityDbContext<AppUser, Role, string>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) 
        : base(options)
    {

    }
    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<Notice>()
            .Property(p => p.NoticeId)
            .ValueGeneratedOnAdd();

        builder.Entity<AppUser>()
            .Property(p => p.Id)
            .ValueGeneratedOnAdd();
        builder.Entity<Role>()
            .Property(p => p.Id)
            .ValueGeneratedOnAdd();
    }
    public DbSet<Notice> Notices { get; set; }
}
public类ApplicationDbContext:IdentityDbContext
{
公共应用程序DBContext(DbContextOptions选项)
:基本(选项)
{
}
模型创建时受保护的覆盖无效(ModelBuilder)
{
builder.Entity()
.Property(p=>p.NoticeId)
.ValueGeneratedOnAdd();
builder.Entity()
.Property(p=>p.Id)
.ValueGeneratedOnAdd();
builder.Entity()
.Property(p=>p.Id)
.ValueGeneratedOnAdd();
}
公共DbSet通知{get;set;}
}
在将通知植入数据库并向其分配用户后,我无法访问AppUser属性。
我已经查看了数据库,可以看到Notices表中有适当的AppUserId列和正确的ID。然而,每次我试图从数据库上下文访问通知的AppUser属性时,它总是为null。我怎样才能修好它?谢谢大家!

您创建了实体框架不知道如何处理的单向关系。我假设如果您查看数据库,您将不会在表上看到外键

请尝试将以下内容添加到AppUser类:

Public ICollection<Notice> Notices { get; set; }
公共ICollection通知{get;set;}
这应该允许实体框架识别关系的双方


我假设每个应用程序用户可能都有很多注意事项。

您创建了一个Entity Framework不知道如何处理的单向关系。我假设如果您查看数据库,您将不会在表上看到外键

请尝试将以下内容添加到AppUser类:

Public ICollection<Notice> Notices { get; set; }
公共ICollection通知{get;set;}
这应该允许实体框架识别关系的双方


我假设每个应用程序用户可能都有许多通知。

如果与此类似,您没有显示如何从
通知访问
AppUser

var notice = dbContext.Notices
                 .First(n => n.NoticeId == id); // get the notice without AppUser

然后,
notice.AppUser
将为
null
,因为您没有明确地包含要加入结果的属性

你可以这样做:

var notice = dbContext.Notices
                 .Include(n => n.AppUser)
                 .First(n => n.NoticeId == id); // get the notice with AppUser


您没有显示如何从
通知
访问
AppUser
,如果与此类似:

var notice = dbContext.Notices
                 .First(n => n.NoticeId == id); // get the notice without AppUser

然后,
notice.AppUser
将为
null
,因为您没有明确地包含要加入结果的属性

你可以这样做:

var notice = dbContext.Notices
                 .Include(n => n.AppUser)
                 .First(n => n.NoticeId == id); // get the notice with AppUser


我曾尝试将其添加到AppUser类中,然后当我尝试进行迁移时,他没有看到db结构中的任何更改。因此,我删除了整个数据库和迁移,并从初始迁移开始。我仍然无法从通知实体访问AppUser。AppUserId外键位于通知表中,并且已正确分配-与之前相同:/I我尝试将其添加到AppUser类中,然后当我尝试进行迁移时,他在db结构中未看到任何更改。因此,我删除了整个数据库和迁移,并从初始迁移开始。我仍然无法从通知实体访问AppUser。AppUserId外键位于通知表中,并且已正确分配-与之前相同:/OK,我是通过存储库执行的,它确实没有加入属性。我很清楚,谢谢你!好的,我是通过存储库做的,它确实没有加入属性。我很清楚,谢谢你!