2017-08-07 26 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