【发布时间】:2013-04-07 22:23:40
【问题描述】:
我有一张位置表、一张服务分配表和一张服务表。位置和服务通过服务分配联系在一起。在位置创建中,我希望能够通过复选框为其分配服务。
但我无法访问位置创建视图中的服务。
位置模型
namespace LocApp.Models
{
public class Location
{
public int id { get; set; }
public string name { get; set; }
public bool active { get; set; }
public virtual ICollection<ServiceAssignment> ServiceAssignment { get; set; }
}
}
服务模式
namespace LocApp.Models
{
public class Service
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public bool active { get; set; }
public string icon { get; set; }
public virtual ICollection<ServiceAssignment> ServiceAssignment { get; set; }
}
}
服务分配
namespace LocApp.Models
{
public class ServiceAssignment
{
public int id { get; set; }
public int locationID { get; set; }
public int serviceID { get; set; }
public virtual Location Location { get; set; }
public virtual Service Service { get; set; }
}
}
在我的上下文类中,我没有映射任何关系,因为根据我看过的文章和屏幕截图,实体框架会为我解决这些问题。
所以这里的主要问题是:
我想在位置创建视图中访问 service.name 和 id 以创建复选框,以便在我保存新位置时可以将位置 id 和该服务 id 保存在数据库中。
这里的诀窍是位置和服务具有多对多的关系。
service assignment id.
Location id -> service assignment location id
service assignment service id <- service id
以上是服务分配表的基本布局。位置服务的所有关系都是通过这个表来处理的。
【问题讨论】:
-
我猜
ServiceAssignment.ServiceAssignment的属性是错字?还是原因? -
你能说得更具体些吗?我看过的每个教程都会做类似
public virtual Book Book { get; set; } -
应该是
public virtual Service Service { get; set; },因为它是ServiceAssignment和Location之间的连接点。 -
是的,我改变了它,我现在正在测试它是否有效,并且在测试时我仍然无法访问,当我这样做时:
@Html.CheckBoxFor(model => model.我可以访问 ServiceAssignment 而没有其他...
标签: asp.net-mvc-3 entity-framework razor