从昨天开始,我正在为这个奇怪的错误而苦苦挣扎。本地主机部署工作正常,但在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