2017-04-07 194 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