【问题标题】:Entity Framework - Validation error on entity read at Azure实体框架 - 在 Azure 读取实体时出现验证错误
【发布时间】:2017-08-07 06:41:48
【问题描述】:

从昨天开始,我一直在与那个奇怪的错误作斗争。本地主机部署工作正常,但在 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>();
    }

    ...
}

应用程序用户

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 数据库克隆到 localhost 后,对我来说更陌生的是它也可以正常工作。

【问题讨论】:

标签: c# asp.net-mvc azure entity-framework-6


【解决方案1】:

根据你的代码,我在我的电脑上开发了一个测试demo,效果很好。

我建议你可以新建一个azure sql数据库,直接在本地使用它的连接字符串再次测试。

关于我的测试演示的更多细节,您可以参考以下代码:

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

}

钱包.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));
    }


}

家庭控制器:

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

结果:

如果这仍然产生错误,请提供错误信息的详细信息。

【讨论】:

  • 这很奇怪,因为我完全改变了它。我没有为钱包和用户使用相同的主键,而是将自己的主键添加到钱包(WalletId)和ApplicationUser的外键,然后我制作了流畅的api映射,现在一切正常。
猜你喜欢
  • 1970-01-01
  • 2010-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-19
  • 1970-01-01
相关资源
最近更新 更多