Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.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# 无法从具有Microsoft标识的角色Id访问实体数据_C#_Sql Server_Entity Framework_Asp.net Identity - Fatal编程技术网

C# 无法从具有Microsoft标识的角色Id访问实体数据

C# 无法从具有Microsoft标识的角色Id访问实体数据,c#,sql-server,entity-framework,asp.net-identity,C#,Sql Server,Entity Framework,Asp.net Identity,我正在尝试根据角色Id访问有关研讨会日程安排请求的信息 该计划可以由2个角色(公共和内部)制定,我需要根据角色Id获取时间范围内的计数 问题是生成的查询正在表“FROM[dbo].[IdentityUserRoles]”中搜索,我有一个名为“AspNetUserRoles”的表,因此我得到了一个错误“无效的对象名[dbo].[IdentityUserRoles]” 我的计划请求模型: [Table("ScheduleRequests")] public class ScheduleRequest

我正在尝试根据角色Id访问有关研讨会日程安排请求的信息

该计划可以由2个角色(公共和内部)制定,我需要根据角色Id获取时间范围内的计数

问题是生成的查询正在表“FROM[dbo].[IdentityUserRoles]”中搜索,我有一个名为“AspNetUserRoles”的表,因此我得到了一个错误“无效的对象名[dbo].[IdentityUserRoles]”

我的计划请求模型:

[Table("ScheduleRequests")]
public class ScheduleRequest
{
    [Key]
    public int Id { get; set; }
    public DateTime DateCreated { get; set; }
    public string User_Id { get; set; }
    [ForeignKey("User_Id")]
    public virtual ApplicationUser User { get; set; }
}
我的应用程序用户模型:

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

        // Add custom user claims here
        return userIdentity;
    }
}
公共类应用程序用户:IdentityUser
{
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
}
我获取计数的方法:

public int GetScheduleRequestsByUserRoleByTimeframe(string roleId, DateTime dateFrom, DateTime dateTo)
{
    dateTo = dateTo.AddDays(1);

    var dbContext = new DbContext();

    var query = dbContext.ScheduleRequests.Where(s => s.DateCreated >= dateFrom && s.DateCreated < dateTo);

    if(!string.IsNullOrEmpty(roleId))
    {
        query = query.Where(s => s.User.Roles.Select(r => r.RoleId).Contains(roleId));
    }
    else
    {
        query = query.Where(s => s.User_Id == null);
    }

    return query.Count();
}
public int GetScheduleRequestsByUserRoleByTimeframe(字符串roleId、DateTime dateFrom、DateTime dateTo)
{
dateTo=dateTo.AddDays(1);
var dbContext=new dbContext();
var query=dbContext.ScheduleRequests.Where(s=>s.DateCreated>=dateFrom&&s.DateCreateds.User.Roles.Select(r=>r.RoleId.Contains(RoleId));
}
其他的
{
query=query.Where(s=>s.User\u Id==null);
}
返回query.Count();
}
我的数据库上下文:

public DbContext() : base("DB") 
{
    // Tells Entity Framework that we will handle the creation of the database manually for all the projects in the solution
    Database.SetInitializer<DbContext>(null);
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // I have inserted this code base on a solution I found when I started to get Id Errors
    modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
    modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
    modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
}

    public DbSet<ScheduleRequest> ScheduleRequests { get; set; }
}
public DbContext():base(“DB”)
{
//告诉实体框架,我们将为解决方案中的所有项目手动处理数据库的创建
Database.SetInitializer(null);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
//我已经根据我在开始出现Id错误时找到的解决方案插入了这段代码
modelBuilder.Entity().HasKey(l=>l.UserId);
modelBuilder.Entity().HasKey(r=>r.Id);
modelBuilder.Entity().HasKey(r=>new{r.RoleId,r.UserId});
}
公共DbSet ScheduleRequests{get;set;}
}
我还有另一个由Microsoft Identity创建的上下文,但我不知道它的用途:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("DB", throwIfV1Schema: false) { }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}
public类ApplicationDbContext:IdentityDbContext
{
public ApplicationDbContext():base(“DB”,throwifvv1schema:false){}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
}

只需从代码库中删除
DbContext
类,并仅使用
ApplicationDbContext
类作为上下文。由于您可以在
ApplicationDbContext
中添加额外的实体,因此在应用程序中不需要另一个实体。考虑这一点:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("DB", throwIfV1Schema: false) { }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    // extra entities added here 
    public DbSet<ScheduleRequest> ScheduleRequests { get; set; }
}
public类ApplicationDbContext:IdentityDbContext
{
public ApplicationDbContext():base(“DB”,throwifvv1schema:false){}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
//这里添加了额外的实体
公共DbSet ScheduleRequests{get;set;}
}

只需从代码库中删除
DbContext
类,并仅使用
ApplicationDbContext
类作为上下文。由于您可以在
ApplicationDbContext
中添加额外的实体,因此在应用程序中不需要另一个实体。考虑这一点:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("DB", throwIfV1Schema: false) { }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    // extra entities added here 
    public DbSet<ScheduleRequest> ScheduleRequests { get; set; }
}
public类ApplicationDbContext:IdentityDbContext
{
public ApplicationDbContext():base(“DB”,throwifvv1schema:false){}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
//这里添加了额外的实体
公共DbSet ScheduleRequests{get;set;}
}

你好,谢谢!是否必须复制所有的OnMealDebug创建代码?不,当一个类继承自<代码> IdditBraveX/<代码>时,它已经有了。现在我得到了一个错误:“支持AptudioDbValue'上下文的模型自从创建数据库以来已经发生了变化。考虑使用代码第一次迁移来更新数据库()。”@帕特里克,这是正常的。因为数据库架构已更改。如果数据库中没有任何重要数据,只需将其删除即可。EF将为您创建一个新的。或者如果你不能放下它。然后考虑启用迁移。我不使用代码第一或迁移,我只使用代码“数据库。SetInitializer(NULL);”并且我认为它解决了问题:所以,如果我理解,问题不是从IdentityDbContext继承。现在我得到了“来自[dbo].[AspNetUserRoles]”!:)问题解决了!非常感谢你!嗨,谢谢!是否必须复制所有的OnMealDebug创建代码?不,当一个类继承自<代码> IdditBraveX/<代码>时,它已经有了。现在我得到了一个错误:“支持AptudioDbValue'上下文的模型自从创建数据库以来已经发生了变化。考虑使用代码第一次迁移来更新数据库()。”@帕特里克,这是正常的。因为数据库架构已更改。如果数据库中没有任何重要数据,只需将其删除即可。EF将为您创建一个新的。或者如果你不能放下它。然后考虑启用迁移。我不使用代码第一或迁移,我只使用代码“数据库。SetInitializer(NULL);”并且我认为它解决了问题:所以,如果我理解,问题不是从IdentityDbContext继承。现在我得到了“来自[dbo].[AspNetUserRoles]”!:)问题解决了!非常感谢你!