2017-08-07 44 views
0

从昨天开始,我正在为这个奇怪的错误而苦苦挣扎。本地主机部署工作正常,但在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数据库克隆到本地主机后,对我来说更陌生的是它也能正常工作。

+0

尝试检查验证错误。请参阅@wizzardz回答:https://stackoverflow.com/questions/17020947/how-do-i-check-entityvalidationerrors-when-validation-fails – Rumpelstinsk

+0

我写道,它是唯一的用户名验证错误,但它在阅读过程中会怎样? – Sajgoniarz

回答

0

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

我建议你可以创建一个新的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; } 

} 

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

结果:

enter image description here

如果这仍然产生了错误,请有关错误消息的详细信息。

+0

它很奇怪,因为我改变了它。我为钱包和用户添加了自己的主键(WalletId)和外键,然后我做了流畅的api映射,现在一切正常。 – Sajgoniarz