开发者

Using MVC 3 Entity Framework Code-First, how do you create a new "many" object in a one-to-many relationship?

开发者 https://www.devze.com 2023-03-27 09:33 出处:网络
I\'m sure that this is very straight-forward, however, after scouring the net I can\'t find anything to set me straight.

I'm sure that this is very straight-forward, however, after scouring the net I can't find anything to set me straight.

I am using ASP.NET MVC 3 with the Entity Framework Code-first modeling.

I have a simple one-to-many relationhip between a Calendar and an Event (For each calendar there can be 0 or more Events).

Simply put, I am confused as to how to create/insert a new event associated with a calendar.

My entity domain definitions are:

public class Calendar
{
    [Key]
    public long id { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public virtual ICollection<Event> events { get; set; }
} //class

public class Event
{
    [Key]
    public long id { get; set; }          
    public string title { get; set; }
    public string description { get; set; }
    public DateTime startdate { get; set; }
    public DateTime enddate { get; set; }
    public int importance { get; set; }
} //class

I have created a page with a link to "Create a new event" that passes the Calendar id to the CalendarController action named Create (/localhost:xxxxx/Calendar/Create/1).

I am stuck at this point as with code-first, I do not have the calendar_id (or something like it) as a specified member of the Event class to associate the event to a calendar. This does exist in the database as the column was generated by the code-first engine.

I tried creating a ViewModel combining the calendar id and an Event object, however, when I return from the Create.cshtml the calendar id was reset from 1 to 0, although the event information was valid.

In closing, in a one-to-many relationship (as above), how do you create a new event (the many side) with a calendar (the one side)? I'm sure it is quite simple, and I'm missing something, but any and all help will be appreciated.

/****************/

Here is a bit more detail that I hope will add clarification to my problem.

  1. A user selects a Calendar (calendar_id, let's say it is 1).
  2. On a detail page for that particular calendar, there is a link to a controller/action (CalendarController/CreateEvent) to create a new event associated with that particular calendar. The created route is Calendar/CreateEvent/1 (create a new event for calendar with id==1).
  3. In the CreateEvent action, I now have the id for the calendar instance for the new event.

This is where I get confused...

The Event object does not have a calendar_id data member as that is created by the code-first engine, so I have nothing to assign the calendar_id that arrived in the route data.

I know that this is a standard situation, but I'm missing something. I need to keep the calendar_id through to the [HttpPost]CreateEvent. From there, I can simply find the proper calendar (using calendar_id), take the info from the new Event form (in CreateEvent.cshtml) to create a new event, and simply call Calendar.Add(newEvent).

How do I keep the calendar_id (or the calendar for that matter) through 开发者_JAVA技巧the whole GET/form/POST cycle?

Thanks again for any help.

/****************/ I re-tried the possible solution mentioned below to add a virtual Calendar and CalendarId to the Event class. I changed the action parameter accepted to an Event object for both the HttpGet and HttpPost functions. In the HttpGet function I set the CalendarId to that of the Calendar to which the new event will be added (as above,the id == 1). Everything worked fine in the CreateEvent view, however, on the return to the HttpPost action of CreateEvent, the CalendarId was reset to 0.

I'm still missing something... but thank you for the help,and I'm still looking for that spark.


After taking some time to rethink my problem and do a bit more research I have found the following information and solution.

First, a very good tutorial (a bit long, but lots of good detail) is available at: http://www.asp.net/entity-framework/tutorials/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

This tutorial does not delve into, nor use, and Ioc, however, the concepts regarding the entity framework and code first ideas is very helpful.

As for my solution, I did two things: 1. As per the above tutorial, I added two fields to my Event class. I added "long timeline_id" and "virtual Calendar calendar" properties. This gives me a place to keep the timeline_id passed into the CreateEvent controller action. And the virtual Calendar property provides a navigation property back to the Calendar table. 2. I have opted to use a hidden field in the CreateCalendar cshtml form to create a new event. This allows the timeline_id to be stored in the form and then returned to the HttpPost instance of the CreateEvent action.

This solves my problem, however, if someone has a more elegant solution, please let me know.


What I normally do, since I'm much more familiar with what the database table structure should typically look like than what the EF Code First syntax should be is set up my tables, then use the EF Power Tools to reverse engineer the tables into POCOs and mappings.

Using MVC 3 Entity Framework Code-First, how do you create a new "many" object in a one-to-many relationship?


(source: msdn.com)

As for your entities why not just add a Calendar property to your Event class

public class Event
{
    [Key]
    public long id { get; set; }          
    public string title { get; set; }
    public string description { get; set; }
    public DateTime startdate { get; set; }
    public DateTime enddate { get; set; }
    public int importance { get; set; }

    public virtual Calendar Calendar { get; set; }
    public int CalendarId { get; set; }
} //class
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号