本文介绍了如何在实体框架和ASP.NET MVC假外键集合属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在亚历克斯·詹姆斯的博客文章如何假外键属性在.NET 3.5 SP1 ,他解释了如何将外键属性添加到实体对象。

In Alex James' blog post How to fake Foreign Key Properties in .NET 3.5 SP1, he explains how to add a Foreign Key property to an Entity Object.

我用,要得到一个参考/导航属性,在一个强类型的ASP.NET MVC应用程序一个DropDownList工作按如下说明:

I've used that to get a reference/navigation property working with a DropDownList in a strongly-typed ASP.NET MVC application as explained here:

Strongly-Typed与ADO.NET实体框架 ASP.NET MVC

Strongly-Typed ASP.NET MVC with ADO.NET Entity Framework

我还需要处理集合。我可以用泰勒蒜。公司的CheckBoxList,而不是内置的DropDownList。

I need to also handle collections. I can use Tyler Garlick's CheckBoxList rather than the built-in DropDownList.

但我怎么扩大的ObjectContext和我EntityObjects处理一到许多类型的关系?

But how do I extend the ObjectContext and my EntityObjects to handle one-to-many type relationships?

我致以部EntityObject类,包括通用型的的Guid名单一PersonIds财产?如何处理set访问?

Do I extend my Department EntityObject class to include a PersonIds property of type Generic List of Guid? How do I handle the set accessor?

推荐答案

我假设你可以得到所选择的人员标识的插入操作方法,以及所有的人已经在数据库中存在的......因为这种形式很简单创建关系到人民和更新该部门本身,而不是创造新的工作人员。

I'm assuming you can get the selected Person Id's into the Action method, and all the People already exist in the database... because this form is simply creating relationships to People and updating the department itself, not creating new staff.

在你想要做这样的事情(psuedo- code)的情况下:

In that case you want to do something like this (psuedo-code):

// get the department from the form using the model binder
Department updated = ... ;

// get the ids of the selected people from the form too.
var currentlySelectedStaffIds = ...;

// get the original department from the database
// including the originally related staff
Department original = ctx.Departments.Include("Staff")
                      .First(dep => dep.Id = updated.Id);

// get the ids of all the originally related staff
var originalStaffIds = original.Staff.Select(s => s.Id).ToArray();

// get the People to be removed from the department
var removedStaff = (from staff in original.Staff
                   where !currentlySelectedStaffIds.Contains(staff.Id)
                   select staff).ToArray();

// get People added to the department (but attached to the context)
var addedStaff = currentlySelectedStaffIds.Except(originalStaffIds)
                 .Select(id => new Person {Id = id}).ToArray();

// Remove the original staff no longer selected.
foreach(var removed in removedStaff)
{
    original.Staff.Remove(removed);
}

// Attach the 'added' staff and build a new relationship for each one
foreach(var added in addedStaff){
   //Put the staff added to the department in the context
   ctx.AttachTo("People", added);
   // build a new relationship
   original.Staff.Add(added);
}

// Apply the changes from updated to the original entity
ctx.ApplyPropertyChanges("Departments", updated);
ctx.SaveChanges();

这基本上是你需要做的..

This is essentially what you need to do..

希望这有助于

亚历

这篇关于如何在实体框架和ASP.NET MVC假外键集合属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:56