简体   繁体   English

使用 MVC 3 Entity Framework Code-First,如何以一对多的关系创建一个新的“多”object?

[英]Using MVC 3 Entity Framework Code-First, how do you create a new “many” object in a one-to-many relationship?

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.我将 ASP.NET MVC 3 与实体框架代码优先建模一起使用。

I have a simple one-to-many relationhip between a Calendar and an Event (For each calendar there can be 0 or more Events).我在日历和事件之间有一个简单的一对多关系(对于每个日历,可以有 0 个或多个事件)。

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).我创建了一个页面,其中包含指向“创建新事件”的链接,该链接将日历 ID 传递给名为 Create (/localhost:xxxxx/Calendar/Create/1) 的 CalendarController 操作。

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.我被困在这一点上,就像代码优先一样,我没有 calendar_id(或类似的东西)作为事件 class 的指定成员来将事件关联到日历。 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.我尝试创建一个结合日历 ID 和事件 object 的 ViewModel,但是,当我从 Create.cshtml 返回时,日历 ID 从 1 重置为 0,尽管事件信息是有效的。

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).用户选择一个日历(calendar_id,假设它是 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.在该特定日历的详细信息页面上,有一个指向控制器/操作 (CalendarController/CreateEvent) 的链接,用于创建与该特定日历关联的新事件。 The created route is Calendar/CreateEvent/1 (create a new event for calendar with id==1).创建的路由是 Calendar/CreateEvent/1(为 id==1 的日历创建一个新事件)。
  3. In the CreateEvent action, I now have the id for the calendar instance for the new event.在 CreateEvent 操作中,我现在有了新事件的日历实例的 ID。

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.事件 object 没有由代码优先引擎创建的 calendar_id 数据成员,因此我无法分配到达路线数据的 calendar_id。

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.我需要将 calendar_id 保留到 [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).从那里,我可以简单地找到合适的日历(使用 calendar_id),从新事件表单(在 CreateEvent.cshtml 中)获取信息来创建新事件,然后调用 Calendar.Add(newEvent)。

How do I keep the calendar_id (or the calendar for that matter) through the whole GET/form/POST cycle?如何在整个 GET/form/POST 周期中保留 calendar_id(或日历)?

Thanks again for any help.再次感谢任何帮助。

/ * ** * ** * ** * ** * ** * / I re-tried the possible solution mentioned below to add a virtual Calendar and CalendarId to the Event class. / * ** * ** * ** * ** * ** * / 我重新尝试了下面提到的可能解决方案,将虚拟日历和 CalendarId 添加到事件 class。 I changed the action parameter accepted to an Event object for both the HttpGet and HttpPost functions.对于 HttpGet 和 HttpPost 函数,我将接受的操作参数更改为 Event object。 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).在 HttpGet function 中,我将 CalendarId 设置为要添加新事件的日历(如上,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.在 CreateEvent 视图中一切正常,但是,在返回 CreateEvent 的 HttpPost 操作时,CalendarId 被重置为 0。

I'm still missing something... but thank you for the help,and I'm still looking for that spark.我仍然缺少一些东西......但感谢您的帮助,我仍在寻找那个火花。

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.我通常做的,因为我更熟悉数据库表结构通常应该是什么样子,而不是 EF Code First 语法应该是设置我的表,然后使用EF Power Tools将表逆向工程为 POCO和映射。

撤销
(source: msdn.com ) (来源: msdn.com

As for your entities why not just add a Calendar property to your Event class至于您的实体,为什么不将Calendar属性添加到您的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

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首先,一个非常好的教程(有点长,但有很多很好的细节)可以在: http://www.asp.net/entity-framework/tutorials/creating-an-entity-framework-data-model-for -an-asp-net-mvc-应用程序

This tutorial does not delve into, nor use, and Ioc, however, the concepts regarding the entity framework and code first ideas is very helpful.本教程没有深入研究,也没有使用 Ioc,但是,关于实体框架和代码优先思想的概念非常有帮助。

As for my solution, I did two things: 1. As per the above tutorial, I added two fields to my Event class.至于我的解决方案,我做了两件事: 1. 根据上面的教程,我在 Event class 中添加了两个字段。 I added "long timeline_id" and "virtual Calendar calendar" properties.我添加了“long timeline_id”和“virtual Calendar calendar”属性。 This gives me a place to keep the timeline_id passed into the CreateEvent controller action.这给了我一个地方来保存传递给 CreateEvent controller 操作的时间线 ID。 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. 2. 我选择使用 CreateCalendar cshtml 表单中的隐藏字段来创建新事件。 This allows the timeline_id to be stored in the form and then returned to the HttpPost instance of the CreateEvent action.这允许将timeline_id 存储在表单中,然后返回到CreateEvent 操作的HttpPost 实例。

This solves my problem, however, if someone has a more elegant solution, please let me know.这解决了我的问题,但是,如果有人有更优雅的解决方案,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使3个实体之间的一对多关系与Entity Framework Core 2.1一起使用 - 代码优先 - How to make one-to-many relationship between 3 entities work with Entity Framework Core 2.1 - code-first 与代码优先实体框架的一对多关系 - One-to-many relationships with code-first Entity Framework 实体框架代码优先在同一个表上的多对多关系 - Entity Framework Code-first Many to many relationship on the same table 实体框架代码优先:如何在两个表之间创建一对多和一对一的关系? - Entity Framework Code First: How can I create a One-to-Many AND a One-to-One relationship between two tables? 实体框架,代码优先,如何从多对多关系映射中向表添加主键? - Entity Framework, code-first, how to add primary key to table out of many-to-many relationship mapping? 如何首先在实体框架 6 代码中创建可为空的一对多关系 - How to create nullable one-to-many relationship in Entity Framwork 6 code first 代码优先实体框架核心中的一对多一对一 - One to Many with One Main in Code-First Entity Framework Core 如何使用 Entity Framework Core 建立一对多关系? - How do I set up a one-to-many relationship using Entity Framework Core? 实体框架代码优先:如何为这种关系创建一对多? - Entity Framework Code First: How can I create a One-to-Many for this relationships? Code First Entity Framework不会创建一对多关系 - Code First Entity Framework doesn't create one to many relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM