從昨天開始,我正在爲這個奇怪的錯誤而苦苦掙扎。本地主機部署工作正常,但在Azure上部署幾個小時後我得到實體框架 - 在Azure上讀取的實體的驗證錯誤
驗證一個或多個實體失敗。有關更多詳細信息,請參閱「EntityValidationErrors」屬性。
這是當我進入我的控制器登記行動取發生:
[AllowAnonymous]
public ActionResult Register()
{
Wallet stockMarketWallet = walletRepository.GetMarketWallet(); // here it comes
RegisterViewModel vm = new RegisterViewModel();
vm.UserStocks = new List<UserStockViewModel>();
foreach (UserStock stock in stockMarketWallet.OwnedStocks)
{
vm.UserStocks.Add(new UserStockViewModel {
StockId = stock.StockId,
Code = stock.Stock.Code
});
}
return View(vm);
}
內部錯誤信息說,UserApplications不是唯一的用戶名上升ValidationError。
WalletRepository
public class WalletRepository : IWalletRepository
{
private ApplicationContext context;
public WalletRepository()
=> context = ApplicationContext.Create();
public Wallet GetMarketWallet()
{
string stockMarketUserName = ConfigurationManager.AppSettings["StockMarketUsername"];
return context.Wallets.FirstOrDefault(w => w.ApplicationUser.UserName.Equals(stockMarketUserName));
}
...
}
}
電子錢包
public class Wallet
{
[Key, ForeignKey("ApplicationUser")]
public string WalletId { get; set; }
public decimal Founds { get; set; }
public virtual IList<UserStock> OwnedStocks { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public Wallet()
{
OwnedStocks = new List<UserStock>();
}
...
}
ApplicationUser
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public virtual Wallet Wallet { get; set; }
}
將Azure數據庫克隆到本地主機後,對我來說更陌生的是它也能正常工作。
嘗試檢查驗證錯誤。請參閱@wizzardz回答:https://stackoverflow.com/questions/17020947/how-do-i-check-entityvalidationerrors-when-validation-fails – Rumpelstinsk
我寫道,它是唯一的用戶名驗證錯誤,但它在閱讀過程中會怎樣? – Sajgoniarz