Insert new Entity with one to many relationship not creating records in both tables

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I'm using MVC5 and EF6 Code First to create a new Company entity that can have many Contactentities. However, only the Company record is being written to the database.

Models:

public class Company
{
    public virtual int CompanyId { get; set; }

    [Required]
    public virtual string Name { get; set; }

    [Required]
    public virtual Address Address { get; set; }

    [Required]
    public virtual string Phone { get; set; }

    public virtual string Email { get; set; }

    // can have many Contacts
    public virtual IEnumerable<Contact> Contacts { get; set; }
}

public class Contact
{
    public virtual int ContactId { get; set; }

    [Required]
    public virtual string Title { get; set; }

    [Required]
    public virtual string Forename { get; set; }

    [Required]
    public virtual string Surname { get; set; }

    [Required]
    public virtual string Phone { get; set; }

    public virtual string Email { get; set; }

    // belongs to one Company
    public virtual Company Company { get; set; }
}

Controller:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create_POST([Bind(Include = "CallerType,Name,Address,Phone,Email,Contacts")] CompanyViewModel viewModel)
    {
        if (ModelState.IsValid)
        {
            // tried commented code to make sure it wasn't a problem with my repository and contact records aren't saved to database...

            //var context = new EfDbContext();

            //var company = new Company
            //{
            //    Name = viewModel.Name,
            //    Phone = viewModel.Phone,
            //    Address = viewModel.Address,
            //    Email = viewModel.Email
            //};

            //context.Companies.Add(company);

            //var contact = new Contact
            //{
            //    Company = company,
            //    Title = "mr",
            //    Forename= "f",
            //    Surname = "s",
            //    Phone = "132"
            //};

            //var contact2 = new Contact
            //{
            //    Company = company,
            //    Title = "mr",
            
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

Contacts must be an ICollection. EF doesn't support IEnumerable as navigation properties, because it's impossible to add items to them.

This also means that you should change the code that creates contacts:

var contacts = viewModel.Contacts.Select(c => new Contact
{
    Title = c.Title,
    Forename = c.Forename,
    Surname = c.Surname,
    Phone = c.Phone,
    Email = c.Email
}).ToList();  // <= ToList() added

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.