gpt4 book ai didi

asp.net-mvc - 具有 ADO.NET Entity Framework 的强类型 ASP.NET MVC

转载 作者:行者123 更新时间:2023-12-03 23:53:53 26 4
gpt4 key购买 nike

关闭。这个问题需要details or clarity .它目前不接受答案。












想改进这个问题?通过 editing this post 添加详细信息并澄清问题.

8年前关闭。




Improve this question




经过几天的努力,我终于完成了这项工作。

我有一个简单的人员和部门数据库:

ADO.NET Entity Framework Entity Data Model diagram with Department and Person objects http://img39.imageshack.us/img39/1368/edmxdepartmentperson.gif

我可以将强类型的 ASP.NET MVC View 用于引用/导航属性!查看部门列表...

ASP.NET MVC with DropDownList http://img11.imageshack.us/img11/7619/dropdownlistdepartment.gif

我的人/编辑 View 的一部分:

<% using (Html.BeginForm()) {%>
<%= Html.Hidden("Id", Model.Id) %>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Name">Name:</label>
<%= Html.TextBox("Name", Model.Name) %>
</p>
<p>
<label for="DepartmentId">Department:</label>
<%= Html.DropDownList("DepartmentId", new SelectList((IEnumerable)ViewData["Departments"], "Id", "Name"))%>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>

我的个人 Controller 的一部分:
//
// GET: /Person/Edit/5

public ActionResult Edit(Guid id)
{
ViewData["Departments"] = ctx.Department;
Person model = (from Person p in ctx.Person
where p.Id == id
select p).FirstOrDefault();
return View(model);
}

//
// POST: /Person/Edit

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Person model)
{
ctx.AttachUpdated(model); //extension
ctx.SaveChanges();
return RedirectToAction("Index");
}

为了让它工作,我用一个新的 DepartmentId 属性扩展了 Person EntityObject。
using System;
using System.Data;
using System.Data.Objects.DataClasses;

namespace ProjectName.Models
{
public partial class Person : EntityObject
{
public Guid DepartmentId
{
get
{
try
{
return (Guid)this.DepartmentReference.EntityKey.EntityKeyValues[0].Value;
}
catch
{
return Guid.Empty;
}
}
set
{
this.DepartmentReference.EntityKey = new EntityKey("JunkEntities.Department", "Id", value);
}
}
}
}

我使用新的 AttachUpdated 和 ApplyReferencePropertyChanges 方法扩展了 Entity Framework ObjectContext:
using System;
using System.Data;
using System.Data.Objects;
using System.Data.Objects.DataClasses;

public static class EntityFrameworkExtensionMethods
{

public static void AttachUpdated(this ObjectContext ctx, EntityObject objectDetached)
{
if (objectDetached.EntityKey == null)
{
String entitySetName = ctx.DefaultContainerName + "." + objectDetached.GetType().Name;
Guid objectId = (Guid)objectDetached.GetType().GetProperty("Id").GetValue(objectDetached, null);
objectDetached.EntityKey = new System.Data.EntityKey(entitySetName, "Id", objectId);
}
if (objectDetached.EntityState == EntityState.Detached)
{
object currentEntityInDb = null;
if (ctx.TryGetObjectByKey(objectDetached.EntityKey, out currentEntityInDb))
{
ctx.ApplyPropertyChanges(objectDetached.EntityKey.EntitySetName, objectDetached);
ctx.ApplyReferencePropertyChanges((IEntityWithRelationships)objectDetached,
(IEntityWithRelationships)currentEntityInDb); //extension
}
else
{
throw new ObjectNotFoundException();
}
}
}

public static void ApplyReferencePropertyChanges(this ObjectContext ctx, IEntityWithRelationships newEntity, IEntityWithRelationships oldEntity)
{
foreach (var relatedEnd in oldEntity.RelationshipManager.GetAllRelatedEnds())
{
var oldRef = relatedEnd as EntityReference;
if (oldRef != null)
{
var newRef = newEntity.RelationshipManager.GetRelatedEnd(oldRef.RelationshipName, oldRef.TargetRoleName) as EntityReference;
oldRef.EntityKey = newRef.EntityKey;
}
}
}

}

我只是想在这里记录我的进步。请提出改进​​建议。

谢谢:
  • Alex James
  • Cesar de la Torre
  • Griff Townsend
  • Steve Willcock
  • jrista
  • Tomas Lycken
  • Thomas Levesque
  • Danny Simmons
  • Stefan Cruysberghs
  • 最佳答案

    我已经开始使用 ASP.NET MVC,这就是我遇到这个线程的原因,所以我不确定你是否还在检查改进。

    我不喜欢将新属性添加到 Entity Framework 上的部分类的想法,因为它不允许进行太多更改。
    尝试像这样标记您的部门下拉菜单“Department.Id”

    <p>
    <label for="Department.Id">Department:</label>
    <%= Html.DropDownList("Department.Id", new SelectList((IEnumerable)ViewData["Departments"], "Id", "Name"))%>
    </p>

    MVC 框架的 ModelBinding 将获取该值并将其应用于“部门”导航属性的“Id”属性。我发现 Department 的其他值都是空的,但这并不重要。现在您可以检索正确的部门实体并将其应用于在模型中创建的新人员实体的部门导航属性绑定(bind)到您的操作参数,例如:
    newPerson.Department = ctx.Department.First(d => d.DepartmentId == newPerson.Department.Id);

    通过这种方式,您根本不需要为它应该具有的属性更新您的实体。

    关于asp.net-mvc - 具有 ADO.NET Entity Framework 的强类型 ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/922402/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com