Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP:Radiobuttonlist/asp:RadioButtonListItem custom styling 1 18
C# Single Form 8 29
Web Form VB.Net  import CSV 4 27
ASP.NET MVC 2 0
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

25 Experts available now in Live!

Get 1:1 Help Now