我首先使用实体框架代码在用户上创建小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; }
}
这是我的班级,任何人都知道如何做到并解决这个错误。
答案 0 :(得分:3)
问题出在这里
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"))
);
}
答案 1 :(得分:0)
我解决了运行此sql命令的错误
create view myschema.ApplicationUser as select * from myschema.AspNetUsers
dbraillon的解决方案对我产生了新的错误:
无效的对象名称dbo.AspNetUsers
因为我的AspNetUsers表不在dbo模式中,所以我需要覆盖默认的onModelCreating