Использование ссылки на объект в Entity Framework - PullRequest
0 голосов
/ 20 августа 2009

Я хочу создать новый объект со ссылкой FK на объект в другой таблице. Это просто простое представление create, но я не знаю, как ссылаться на сущность Research с ID == id.

    public ActionResult Create(int id)
    {
        SurveyPaper surveyPaper = new SurveyPaper();

        return View(surveyPaper);
    } 

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(int id, FormCollection collection)
    {
        var surveyPaper = new SurveyPaper();

        try {
            UpdateModel(surveyPaper);
            surveyPaper.Research.ResearchID = id;
            _r.AddSurveyPaper(surveyPaper);

            return RedirectToAction("Details", new { id = surveyPaper.SurveyPaperID });
        }
        catch {
           return View(surveyPaper);
        }
    }

1 Ответ

1 голос
/ 20 августа 2009

В .NET 3.5 SP 1:

// obviously, substitute your real entity set and context name below
surveyPaper.ResearchReference.EntityKey =
   new EntityKey("MyEntities.ResearchEntitySetName", "ResearchID", id);

В .NET 4.0 вместо этого используйте ассоциации FK.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...