From yesterday i am struggling with that strange error. Localhost deploy works fine, but few hours after deploy on Azure i get
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
It's happen when i enter to my controller registration action on fetching :
[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);
}
Internal Error details says that UserApplications not unique username is rising 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));
}
...
}
}
Wallet
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; }
}
What is even stranger to me after cloning Azure database to localhost it works fine too.
According to your codes, I have developed a test demo on my computer, it works well.
I suggest you could create a new azure sql database and use its connection string directly in the local to test again.
More details about my test demo, you could refer to below codes:
IdentityModels.cs:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public virtual Wallet Wallet { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public DbSet<Wallet> Wallets { get; set; }
public DbSet<UserStock> UserStocks { get; set; }
}
Wallet.cs
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>();
}
}
WalletRepository.cs
public class WalletRepository
{
public ApplicationDbContext context;
public WalletRepository() { context = ApplicationDbContext.Create(); }
public Wallet GetMarketWallet()
{
string stockMarketUserName = "The user name";
return context.Wallets.FirstOrDefault(w => w.ApplicationUser.UserName.Equals(stockMarketUserName));
}
}
HomeController:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
//Add test record
public ActionResult About()
{
ApplicationDbContext d1 = new ApplicationDbContext();
ApplicationUser user = d1.Users.FirstOrDefault(w => w.UserName.Equals("UserName"));
Wallet w1 = new Wallet();
w1.ApplicationUser = user;
w1.Founds = 300;
UserStock u1 = new UserStock();
u1.id = 1;
List<UserStock> l1 = new List<UserStock>();
l1.Add(u1);
w1.WalletId = user.Id;
d1.Wallets.Add(w1);
d1.SaveChanges();
ViewBag.Message = "Add Completed";
return View();
}
//Call the Repository to get the value
public ActionResult Contact()
{
WalletRepository walletRepository = new WalletRepository();
var result = walletRepository.GetMarketWallet();
ViewBag.Message = "WalletId : " + result.WalletId;
return View();
}
}
Result:
If this is still generate the error, please the details information about the error message.