2014-10-06 41 views
10

我看到與this問題相同的問題,但在該處顯示的方案似乎不適用,因此我認爲我有不同的問題。事實上,我看到了幾個類似的問題,每個問題都有不同的原因和解決方案,所以我認爲這個錯誤必須由高層次引起。這說...實體類型IdentityUser不是當前上下文的模型的一部分

我有一個EF代碼優先的數據庫模型,我試圖用IdentityUser來擴展我的MVC 5網站的標準註冊。

我有我的擴展UserModel

namespace MyMvcSite.Models { 
    public class UserModel :IdentityUser { 
     public string BillingId { get; set; } 
     public virtual ICollection<DatabaseModel> Databases { get; set; } 
} 

我的背景:

using MyMvcSite.Models; 
namespace MyMvcSite.Web { 
    public class AuthContext : IdentityDbContext<UserModel> { 
     public AuthContext() : base("AuthContext") { 

     } 
    } 
} 

現在,當我執行的代碼以註冊用戶:

public async Task<IdentityResult> RegisterUser(UserModel user) { 
    user.Email = user.UserName; 
    var result = await _userManager.CreateAsync(user); 

    return result; 
} 

我得到的錯誤:The entity type IdentityUser is not part of the model for the current context.我無法弄清楚這個錯誤的含義,因爲它看起來 - 對我來說 - l艾克我擁有一切正確的。任何人都可以告訴可能會出錯嗎?

我知道我的connectionString AuthContext是正確的,因爲我之前使用過它。

+0

你如何實例化你的'_userManager'及其'context'?這似乎可能有所幫助:http://stackoverflow.com/questions/23893710/the-entity-type-applicationuser-is-not-part-of-the-model-for-the-current-context – 2014-10-06 01:06:54

+0

我的_userManager '初始化爲:'_userManager = new UserManager (新的UserStore (_ ctx));'。我把它改成了'_userManager = new UserManager (新的UserStore (_ctx));',它解決了它!發佈這個答案,我會接受。感謝您指點我正確的方向! – Brett 2014-10-06 01:23:19

回答

13

當您使用具有ASP.NET Identity的自定義用戶類時,必須確保您在實例化中明確指定UserManagerUserStore的自定義用戶類類型<T>

private UserManager<UserModel> _userManager; 

public AccountController() 
{ 
    AuthContext _ctx = new AuthContext(); 

    UserStore<UserModel> userStore = new UserStore<UserModel>(_ctx); 
    _userManager = new UserManager<UserModel>(userStore);  
} 

或在較短的形式(如您的回覆意見):

private UserManager<UserModel> _userManager; 

public AccountController() 
{ 
    AuthContext _ctx = new AuthContext();  
    _userManager = new UserManager<UserModel>(new UserStore<UserModel>(_ctx));  
} 

如果類型被允許時,要使用自定義類,你將體驗您所報告的錯誤默認爲IdentityUser

+0

這樣做了!謝謝! – Brett 2014-10-10 14:17:38

+0

謝謝,@Brett – 2016-01-10 11:10:33

+0

+100它的工作! – 2017-09-17 07:27:23

5

當我將DI引入到我的項目中時,出現此錯誤。使用AutoFac和身份,我不得不添加以下內容:builder.RegisterType<ApplicationDbContext>().As<DbContext>().InstancePerLifetimeScope();

沒有這一點,當AutoFac是創造我UserStore情況下,它是使用默認的構造函數UserStore()這將創建一個新的IdentityDbContext,不是我ApplicationDbContext

用這條線,UserStore(DbContext context) ctor被叫,用我的ApplicationDbContext

7

我遇到了同樣的問題,我記得在MVC4中使用SimpleMembership有類似的問題。

我正在做數據庫第一次開發,所以我有一個EDMX文件。事實證明,ASP.NET Identity不喜歡在生成.edmx模型文件時創建的連接字符串。如果您使用的是。 EDM連接字符串在:base(「EDMConnString」)中,您很可能會遇到此問題。

我通過創建一個標準的連接字符串來解決這個問題,該連接字符串指向ASP.NET身份表所在的數據庫(在我的情況下是同一個數據庫),在base中使用該連接字符串,並且它工作正常。

像這樣的事情

<add name="IdentityConnection" connectionString="data source=THEJUS\SQLSERVER2014;initial catalog=IdentitySample;integrated security=True;MultipleActiveResultSets=True;App=IdentitySample.Admin" providerName="System.Data.SqlClient" /> 
+3

是的。這解決了我的問題。有2個連接字符串需要。一個用於身份的東西,一個用於EF。如果您查看Web.Config,您會看到一個默認連接,然後在安裝過程中配置它時,會出現一個EF。您可以輕鬆構建虛擬MVC5並添加EF以查看它們並在數據庫相同的情況下使用它們。 我的問題是由於稍後我添加了EF的MVC 5應用程序引起的(數據第一次)。然後我刪除了「DefaultConnection」,認爲我不需要它。所以Identity使用base中引用的連接。 EF使用您在控制器頂部設置的任何實體模型。 – JustJohn 2015-10-27 05:40:16

+0

MyModelEntities _db = new MyModelEntities(); – JustJohn 2015-10-27 05:44:19

+1

謝謝@JustJohn,這是最終對我有用的東西! – GabeMeister 2016-10-04 13:02:48

相關問題