Entity Frameworkを使用しているASP.NET MVC RC2アプリケーションで作業しています。ASP.NET MVCビューから返されたときにオブジェクトに関連するエンティティを保持する方法
これは私のエンティティダイアグラムです。私のリポジトリに
Fi.1 http://img.skitch.com/20090308-t5mepq1hk1mju6gmuabrxbrr9c.png
は、私はこのようなエンティティを取得:
public Product GetProduct(int id)
{
return (from c in _entities.ProductSet.Include("User")
where c.Id == id
select c).FirstOrDefault();
}
マイビュー:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SampleApp.Models.Product>" %>
編集製品
<h2>Edit product</h2>
<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Id">Id:</label>
<%= Model.Id %>
</p>
<p>
<label for="Title">Title:</label>
<%= Html.TextBox("Title", Model.Title) %>
<%= Html.ValidationMessage("Title", "*") %>
</p>
<p>
<label for="Body">Body:</label>
<%= Html.TextBox("Body", Model.Body) %>
<%= Html.ValidationMessage("Body", "*") %>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
問題と、私の質問は:「製品」の実体は、ポスト上の観点から返された場合、リンクされた実体「ユーザー」はnullです。それはなぜですか、これを回避する方法はありますか?
fig 2 http://img.skitch.com/20090308-minhefyq6gm9d7qhiihak6rqn6.png
私はこれが、「新しい」というオブジェクトがビューから来ているケースだと思いました。自分のシナリオでユーザーを変更する必要はありません。ユーザーは、製品を誰が作成したかを追跡する関係にのみあります。この情報をありがとう、私はそれを回避します。 – jesperlind