When I was using Entity framework code first, I encountered an error when I tried to create an entity into database.
The entity is:
[Table("EmployeeProfile")]
public partial class EmployeeProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int EmployeeProfileID { get; set; }
[ForeignKey("Employee")]
public int EmployeeID { get; set; }
public virtual Employee Employee { get; set; }
[ForeignKey("Profile")]
public int ProfileID { get; set; }
public virtual Profile Profile { get; set; }
[Required]
[StringLength(255)]
public string ProfileValue { get; set; }
}
When creating the entity, some entities have the ProfileValue="", this causes the EntityValidationException with the detailed message "OriginalValues cannot be used for entities in the Added state". I want to allow the ProfileValue to be empty but also make it a require field. I found out that the Required attribute has a boolean parameter called "AllowEmptyStrings". Set this value to true will enable entity framework to create EmployeeProfile entity with empty string ProfileValue.
The entity is:
[Table("EmployeeProfile")]
public partial class EmployeeProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int EmployeeProfileID { get; set; }
[ForeignKey("Employee")]
public int EmployeeID { get; set; }
public virtual Employee Employee { get; set; }
[ForeignKey("Profile")]
public int ProfileID { get; set; }
public virtual Profile Profile { get; set; }
[Required]
[StringLength(255)]
public string ProfileValue { get; set; }
}
When creating the entity, some entities have the ProfileValue="", this causes the EntityValidationException with the detailed message "OriginalValues cannot be used for entities in the Added state". I want to allow the ProfileValue to be empty but also make it a require field. I found out that the Required attribute has a boolean parameter called "AllowEmptyStrings". Set this value to true will enable entity framework to create EmployeeProfile entity with empty string ProfileValue.
Comments