Entity Framework - Validation error on entity read at Azure

asp.net-mvc azure c# entity-framework-6

Question

I've been having trouble with that odd issue since yesterday. Localhost deploy works OK, however shortly after deploying to Azure, I get

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

When I Enter My Controller Registration Action on Fetching, This Occurs:

    [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' non-unique username is causing a growing ValidationError, according to internal error data.

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; }
}

Even weirder, after replicating an Azure database to localhost, it continues to function normally.

1
0
8/7/2017 6:41:48 AM

Popular Answer

I created a test demo on my machine using your scripts, and it works well.

In order to retest, I advise you to build a fresh Azure SQL database and utilise its connection string directly in the local.

You may use the codes below for more information about my test demo:

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:

enter image description here

If the problem still occurs, kindly provide further information regarding the error message.

0
8/8/2017 5:48:32 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow