Database Validation Errors. Allow NULL in one table, but not in a linked (normalized) table

I searched for the answer to this question for 2 days. I'm not even sure exactly what is wrong, but I think I could identify a possible culprit.

My program is a tracker / database manager that will track the equipment in my company. The user can request, create and edit equipment. The database query is working right now, but when I try to add or update the database, the program throws validation errors (I will discuss this below).

I have a hardware class that has many attributes with a zero value (because the user may not know many of these properties when creating a new record, we left almost all the attributes with a zero value) and connected to it (using foreign keys and lazy loading / "virtual") to several other tables that were made to help normalize the database.

Here are some short codes to help:

Equipment Model:

public class Equipment
{
    public int EquipmentId { get; set; } //required

    public string Company { get; set; } //required

    public bool Verified { get; set; } //required

    (...other non-required attributes...)


    //The following two attributes are where my problems are I believe
    [ForeignKey("PAU")]
    public int PAUId { get; set; } //required and Foreign Key    

    //This is not required (but is FK to the related table) if the user doesn't know
    [ForeignKey("Division")]
    public Nullable<int> DivisionId { get; set; }


    //These are the lazy loading connections to the other tables in the database

    public virtual Division Division { get; set; }

    public virtual PAU PAU { get; set; }
}

Unit model: This table contains only 5 different units in our company and the corresponding identifiers.

public class Division
{
    public Division()
    {
        this.Equipments = new HashSet<Equipment>();
    }

    //These are non-nullable/required fields
    public int DivisionId { get; set; }
    public string DivisionName { get; set; }

    public virtual ICollection<Equipment> Equipments { get; set; }
}

PAU Model: This model contains PAU # s and the corresponding description and identifier.

public class PAU
{
    public PAU()
    {
        this.Equipments = new HashSet<Equipment>();
    }

    //These are non-nullable/required fields
    public int PAUId { get; set; }
    public string PAUNumber { get; set; }
    public string PAUDescription { get; set; }

    public virtual ICollection<Equipment> Equipments { get; set; }
}

Hardware Controller:

public class EquipmentController : Controller
{
    TLCP_DEVEntities4 db = new TLCP_DEVEntities4();

    (...)

    public ActionResult Edit(int id)         
    {
        ViewData["divisions"] = new SelectList(db.Equipments.
                                OrderBy(x => x.Division.DivisionName).
                                Select(x => x.Division).ToList().
                                Distinct(), "DivisionId", "DivisionName");

        ViewData["PAUNumbers"] = new SelectList(db.Equipments.
                                 OrderBy(x => x.PAU.PAUNumber).
                                 Select(x => x.PAU).ToList().
                                 Distinct(), "PAUId", "PAUNumber");

        return View(db.Equipments.Find(id)); 
    }

    [HttpPost]
    public ActionResult Edit(Equipment equipment)
    {
        if (ModelState.IsValid)
        {
            db.Entry(equipment).State = EntityState.Modified;                    
            db.SaveChanges();     //fails here                                     
            return RedirectToAction("Maintenance"); 
        }

        ViewData["divisions"] = new SelectList(db.Equipments.
                                OrderBy(x => x.Division.DivisionName).
                                Select(x => x.Division).ToList().
                                Distinct(), "DivisionId", "DivisionName");

        ViewData["PAUNumbers"] = new SelectList(db.Equipments.
                                 OrderBy(x => x.PAU.PAUNumber).
                                 Select(x => x.PAU).ToList().
                                 Distinct(), "PAUId", "PAUNumber");

        return View(equipment);
    }

    public ActionResult AddEquipment()
    {
        ViewData["divisions"] = new SelectList(db.Equipments.
                                OrderBy(x => x.Division.DivisionName).
                                Select(x => x.Division).ToList().
                                Distinct(), "DivisionId", "DivisionName");

        ViewData["PAUNumbers"] = new SelectList(db.Equipments.
                                 OrderBy(x => x.PAU.PAUNumber).
                                 Select(x => x.PAU).ToList().
                                 Distinct(), "PAUId", "PAUNumber");

        return View();
    }

    public ActionResult AddEquipment(Equipment equipment)
    {
        try
        {
            db.Equipments.Add(equipment);
            db.SaveChanges();

            return RedirectToAction("Maintenance"); 
        }
        catch (Exception ex)
        {

            ViewData["divisions"] = new SelectList(db.Equipments.
                                    OrderBy(x => x.Division.DivisionName).
                                    Select(x => x.Division).ToList().
                                    Distinct(), "DivisionId", "DivisionName");

            ViewData["PAUNumbers"] = new SelectList(db.Equipments.
                                 OrderBy(x => x.PAU.PAUNumber).
                                 Select(x => x.PAU).ToList().
                                 Distinct(), "PAUId", "PAUNumber");

            return View();
        }
    }

    (...)
}

, ( ViewData ). , , , :

<%: Html.DropDownListFor(model => model.DivisionId,
       ViewData["divisions"] as SelectList, "")%>

, , , . , , .

: , DivisionId Equipment ( , ), / Equipment . SaveChanges(), , Division null ( DivisionId 0 ). Equipment.DivisionId null, Division Equipment. ? ?

PAU: PAUId Equipment, , PAU.PAUNumber PAU.PAUDescription ( PAU.PAUId 0), . , ( , -).

, , .

  • , Equipment , Id/Foreign Key ?

  • - , , ? , ?

  • , , , (, DivisionName)?

. - .

Edit

PAU ( , Division - , ).

public ActionResult AddEquipment(Equipment equipment)
    {
        try
        {
            equipment.PAU.PAUId = equipment.PAUId;
            equipment.PAU.PAUNumber = db.PAUs.Find(equipment.PAUId).PAUNumber;
            equipment.PAU.PAUDescription = db.PAUs.Find(equipment.PAUId).PAUDescription;


        (...)

    }

. , . , - ?

Division ( Equipment.DivisionId NULL, Division.DivisionId NULL, ), !

+5
1

, , . ! !

0

All Articles