Solved

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

Posted on 2011-02-16
5
1,302 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 39

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 39

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now