Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Commented Unassigned: Validation failed for one or more entities. [1571]

$
0
0
So I have the following Entities :

[Table("UserTbl")]
public abstract class User
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
[Required]
public string Username { get; set; }
[Required]
public DateTime CreatedDate { get; set; }
[Required]
public virtual UserPassword Password { get; set; }
[Required]
public string RegistrationNr { get; set; }
[Required]
public string ContactPerson { get; set; }
[Required]
public string Email { get; set; }
[Required]
public string PhoneNr { get; set; }

[Required]
public bool HasBeenApproved { get; set; }

public DateTime? InactivationDate { get; set; }
}


[Table("UserPasswordTbl")]
public class UserPassword
{
[Required]
[Key]
[ForeignKey("User")]
public int UserId { get; set; }
[Required]
public User User { get; set; }
[Required]
public string Password { get; set; }
[Required]
public string Salt { get; set; }
public DateTime? PasswordChangedDate { get; set; }
public DateTime? LastPasswordFailureDate { get; set; }
public int PasswordFailuresSinceLastSuccess { get; set; }
}


Just a User object which has a property of a password object which is required for it to work.

then when the following method is called:

public string ActivateUser(string encryptedUsername)
{
string decryptedUsername = CryptographyTools.DecryptText(encryptedUsername);
User user = _userRepository.GetUser(decryptedUsername);
if (user != null)
{
user.HasBeenApproved = true;
_userRepository.SaveChanges();
}
return decryptedUsername;
}

a DbEntityValidationException is thrown. The EntityValidationErrors property of the exception suggests that the users Password property is not valid. However(!!), if I put a breakpoint somewhere after retrieving the user object from the repository and before the call to SaveChanges, this exception is not thrown and the entity is successfully updated in the database.

I had to fix this bug by removing the Required attribute from the Password property of the user entity.

I think EF is failing to load my object properly before a call is made to SaveChanges, maybe there is some weird yield return implementation.

//Siavash
Comments: Hi Siamaster, Just a short in the dark: Since I don't see in the code you provided anything that would trigger the loading of the Password property, I imagine if you hit a breakpoint before SaveChanges the debugger could for some reason inspect the property (e.g. if you have expanded the 'user' variable in either the locals or the watch windows). That would be enough to trigger lazy loading and to allow the validation to pass. That said, until know I had the vague memory that we special cased validation attributes on navigation properties to account for the ambiguous semantics of null on a navigation properties (a null reference can either mean that the association is really null or that the association hasn't been loaded yet). What you are describing would prove that I was wrong. Moozzyk may actually remember better how this is supposed to work as we wrote part of that code.

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>