gpt4 book ai didi

c# - 对象名称 'dbo.ApplicationUsers' 无效。?

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:33 25 4
gpt4 key购买 nike

我首先使用实体​​框架代码对用户创建小的 CRUD 操作。现在我需要使用存储过程删除用户,所以我找到许多链接获取如何在 EF 中使用存储过程的解决方案。所以我写这段代码那个。但是我在类中编写此代码和方法 OnModelCreating() 并去登录那个时候出现错误,如“无效的对象名称'dbo.ApplicationUsers'。我评论此方法登录非常有效,但取消注释此代码出现错误。

这是我的课:

  using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

namespace abc.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
// Add custom user claims here
return userIdentity;
}
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}

public virtual DbSet<Users> Users { get; set; }


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

public virtual ObjectResult<List<Users>> GetUserInfo()
{
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<List<Users>>("GetUserInfo");
}

protected override void OnModelCreating(DbModelBuilder modelBuilder) // this method wroite after getting error
{
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 });

modelBuilder.Entity<Users>()
.MapToStoredProcedures(s => s.Delete(u => u.HasName("DeleteUserById", "dbo")
.Parameter(b => b.id, "userid"))
);
}

}
}

这是用户类:

[Table("Users")]
public class Users
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
public long id { get; set; }

public string firstname { get; set; }

public string lastname { get; set; }

public string contactnumber { get; set; }

public DateTime datetime { get; set; }

}

这是我的类(class),任何人都知道如何做到这一点并解决这个错误。

最佳答案

问题就出在这里

protected override void OnModelCreating(DbModelBuilder modelBuilder) // this method wroite after getting error
{
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 });

modelBuilder.Entity<Users>()
.MapToStoredProcedures(s => s.Delete(u => u.HasName("DeleteUserById", "dbo")
.Parameter(b => b.id, "userid"))
);
}

删除前三行并添加

base.OnModelCreating(modelBuilder);

会是这样

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<Users>()
.MapToStoredProcedures(s => s.Delete(u => u.HasName("DeleteUserById", "dbo")
.Parameter(b => b.id, "userid"))
);
}

关于c# - 对象名称 'dbo.ApplicationUsers' 无效。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43273544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com