简体   繁体   中英

Adding a new row to a table with many foregin keys

I have a small problem with setting up relations between tables in entity. Basically I have three models: AppUser , Assignment and CompletedAssignment .

public class AppUser
{
    [Display(Name = "Your Id:")]
    public int ID { get; set; }
    [Display(Name = "First Name")]
    public string FirstName { get; set; }
    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    public virtual ICollection<CompletedAssignment> CompletedAssignments { get; set; }
}

public class Assignment
{   
    [Display(Name="Assignment ID")]
    public int ID { get; set; }
    [Display(Name = "Assignment Name")]
    public string AssignmentName { get; set; }
    [Display(Name = "Assignment Description")]
    [DataType(DataType.MultilineText)]
    public string Description { get; set; } 
    public DateTime CreationDate { get; set; }

    public Assignment(){
        this.CreationDate = DateTime.Now;
    }

    public virtual ICollection<CompletedAssignment> Assignments { get; set; }
}

public class CompletedAssignment
{
    public int ID { get; set; }
    [Display(Name = "Assignment ID")]
    public int AssignmentID { get; set; }
    [Display(Name = "Student ID")]
    public int AppUserID { get; set; }
    public DateTime CompletionDate{get;set;}

    public CompletedAssignment()
    {
        this.CompletionDate = DateTime.Now;
    }

    public virtual Assignment Assignment { get; set; }
    public virtual AppUser AppUser { get; set; }
}

I (think) I have configured a set of relations between them: one to many relation between AppUser and CompletedAssignment , and one to many between Assignment and CompletedAssignment so that I have two foreign keys in the CompletedAssignment table.

The problem occurs when I'm trying to add a new CompletedAssignment to the database with two foreign key Id's supplied by the user.

The error message:

"The INSERT statement conflicted with the FOREIGN KEY constraint \\"FK_dbo.CompletedAssignments_dbo.AppUsers_AppUserID\\". The conflict occurred in database \\"aspnet-name-20160527092700\\", table \\"dbo.AppUsers\\", column 'ID'.\\r\\nThe statement has been terminated."

And code that causes it:

[HttpPost]
public ActionResult Create([Bind(Include = "ID,AssignmentID,UserID,CompletionDate")] CompletedAssignment completedAssignment)
{
    if (ModelState.IsValid)
    {
        db.CompletedAssignments.Add(completedAssignment);

        db.SaveChanges();

        return RedirectToAction("Index");
    }

    return View(completedAssignment);
}

As far I remember from SQL this error means that there has been non-existent key supplied to the query, but I have checked it few times and the keys supplied exist in both tables. I'm kind of struggling to grasp how entity database interaction actually works, so any kind of help will be appreciated:)

AppUserID has no value.

You wrote

[Bind(Include = "ID,AssignmentID,UserID,CompletionDate")]

It should be

[Bind(Include = "ID,AssignmentID,AppUserID,CompletionDate")]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM