2017-04-07 177 views
1

我创建一个使用实体框架代码的用户的小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; } 

} 

这是我班上任何一个知道如何能做到这一点,解决它这个错误。

+0

有什么理由让你同时拥有'ApplicationUser'和'用户'? 'IdentityDbContext'已经定义了一个'DbSet ',为什么不用它呢? – dbraillon

+0

家伙你想说什么idk我是新的EF和idenetity你能给我提示 – coderwill

+0

你的意思modelBuilder.Entity ()这里用户替换ApplicationUsers? – coderwill

回答

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")) 
     ); 
} 
+0

你好,dbraillon你能帮我一个吗? – coderwill