Solved

ASP.NET MVC EF Code First Many-to-Many Insert

Posted on 2011-02-16
5
1,311 Views
Last Modified: 2012-05-11
Hello,

I am using EF Code First approach for my web application. I have two entities, Service Providers and Categories and a many-to-many relationship between them. I managed the lookup table automatically generated by using the following code, but could not manage to insert into the lookup table. I'm getting a null reference error when using the following code. I am looking forward to hearing a solution:

 
In ServiceProvidersController:

        [AcceptVerbs("POST")]
        public ActionResult Create(ServiceProvider newSP)
        {
            if (ModelState.IsValid)
            {
                var categoryId = Request["CategoryID"];
                var catIds = categoryId.Split(',');
                foreach (var catId in catIds)
                {
                    int id = int.Parse(catId);

                    Category category = db.Categories.SingleOrDefault(d => d.CategoryID == id);
                    newSP.Categories.Add(category);
                }
                db.ServiceProviders.Add(newSP);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
 
            return View();
        }

Open in new window


 
public class Category
    {
        public int CategoryID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }

        public ICollection<ServiceProvider> ServiceProviders { get; set; }
    }

Open in new window



 
public class ServiceProvider
    {
        public int ServiceProviderID { get; set; }
        [Required]
        public string Title { get; set; }
        public string Telephone { get; set; }
        public string Fax { get; set; }
        public string Address { get; set; }
        
        public virtual ICollection<Category> Categories { get; set; }       
    }

Open in new window

0
Comment
Question by:bkadirbeyoglu
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 34909533
Would try this:

for each var cat in db.Categories.Where(d => d.CategoryID == id)
                    newSP.Categories.Add(cat);
0
 

Author Comment

by:bkadirbeyoglu
ID: 34910063
ged325, thank you, but it did not work either...
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 34916490
http://naspinski.net/post/Getting-started-with-Linq-To-Entities.aspx

See the section about using .Load . . . I think you need to call that before you add.
0
 

Accepted Solution

by:
bkadirbeyoglu earned 0 total points
ID: 34941794
0
 

Author Closing Comment

by:bkadirbeyoglu
ID: 34986380
I posted the link where my problem was solved.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question