我創建一個使用實體框架代碼的用戶的小crud操作first.now我需要刪除用戶使用存儲過程,所以我找到很多鏈接獲得如何在EF.so中使用存儲過程的解決方案我爲此編寫代碼。但是im在類中寫入這個代碼和方法OnModelCreating()並且去找到這樣的錯誤,如'無效的對象名'dbo.ApplicationUsers'。我評論此方法登錄非常好的工作,但取消註釋此代碼獲取錯誤。對象名稱'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; }
}
這是我班上任何一個知道如何能做到這一點,解決它這個錯誤。
有什麼理由讓你同時擁有'ApplicationUser'和'用戶'? 'IdentityDbContext'已經定義了一個'DbSet',爲什麼不用它呢? –
dbraillon
傢伙你想說什麼idk我是新的EF和idenetity你能給我提示 – coderwill
你的意思modelBuilder.Entity()這裏用戶替換ApplicationUsers? –
coderwill