2017-08-07 14 views
0

昨日から、私はその奇妙なエラーに苦しんでいます。 Localhostのデプロイメントはうまく動作しますが、Azureのデプロイ後数時間はかかりますエンティティフレームワーク - エンティティの検証エラーがアズール

1つ以上のエンティティの検証に失敗しました。詳細は、「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

Azureデータベースをlocalhostにクローンした後も私にはわかりません。

+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 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 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

に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

かなり変わったので、かなり奇妙です。ウォレットとユーザーの両方に同じプライマリキーを使用するのではなく、自分のプライマリキーをWallet(WalletId)とForeignUser for ApplicationUserに追加して、流暢なAPIマッピングを作成しました。 – Sajgoniarz

関連する問題