Disable Entity Framework validation for some specific field on insertion action

asp.net-mvc asp.net-mvc-5 c# entity-framework entity-framework-6

Question

When entering data, I'm attempting to stop the validation of several fields, including passwords. But when I try to insert, I get the following error:

Cannot insert the value NULL into column 'Password', table 'Uni.Models.MyDb.dbo.Students'; column does not allow nulls. INSERT fails. The statement has been terminated.

My reference group:

public class Student
{
    [Key]
    public int StudentId { get; set; }

    [Required(ErrorMessage = "Name Required")]
    public string Name { get; set; }

    public string IdNumber { get; set; }

    [Required(ErrorMessage = "Please Enter Password"), MinLength(5, ErrorMessage = "Password length error")]
    public string Password { get; set; }

    [Required, Compare("Password", ErrorMessage = "Invalid Confirm Password")]
    public string ConfirmPassword { get; set; }

    [Required(ErrorMessage = "Date Of Birth Required")]
    [Column(TypeName = "Date")]
    public DateTime BirthDate { get; set; }


    public virtual Gender Gender { get; set; }
    [Required(ErrorMessage = "Select Gender!")]
    public int GenderId { get; set; }
}

My commanding:

[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public ActionResult add(Student std)
{
    db.Configuration.ValidateOnSaveEnabled = false;

    if (ModelState.IsValidField("Name") && ModelState.IsValidField("GenderId") && ModelState.IsValidField("BirthDate"))
    {
        Common common = new Common();
        std.IdNumber = common.generateUsername("S-");

        db.Students.Add(std);

        db.SaveChanges();
        db.Configuration.ValidateOnSaveEnabled = true;

        return RedirectToAction("Index");
    }

    ViewBag.Genders = new SelectList(db.Genders, "GenderId", "Dari");
    ViewBag.Provinces = new SelectList(db.Provinces, "ProvinceId", "Name");

    return View();
}

I would really appreciate it if someone could point out where I'm going wrong and assist me.

1
0
9/9/2017 10:28:40 AM

Popular Answer

Approach 1:

The password column is necessary, as can be shown by looking at the SQL server database itself. This may be seen in a programme like SQL Server Management Studio or the Visual Studio SQL Server Object Explorer (under the View menu). Before inserting a record with a NULL password, you must configure this field to accept nulls (see the Allow Nulls column below; you must check this box in each column you wish to allow nulls in).

Make sure everyone else involved in the project is aware of the need for this modification.

enter image description here


Approach 2:

Use migrations from Code First. If you haven't previously, you may activate code migrations on your EF project by following this tutorial here https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application. Once you're certain that migrations are enabled, modify the produced migration to permit nulls in Password. E.g:

CreateTable(
            "dbo.Students",
            c => new
                {
                    StudentId = c.Int(nullable: false, identity: true),
                    Name = c.String(nullable: false),
                    IdNumber = c.String(),
                    Password = c.String(nullable: true), // This is the key bit - whether you set the value to nullable. The default is true, but because you marked the field as required nullable will be set to false
                    ConfirmPassword = c.String(nullable: true), // Same as above - by default this is required too. You might want to remove this property from your data object altogether
                    BirthDate = c.DateTime(nullable: false, storeType: "date"),
                    Gender = c.Int(nullable: false),
                    GenderId = c.Int(nullable: false),
                })
            .PrimaryKey(t => t.StudentId);
1
9/9/2017 11:10:04 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow