Asp.net mvc 具有多个表的实体框架ViewModel

Asp.net mvc 具有多个表的实体框架ViewModel,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我正在为Events表构建MVC CRUD页面,在表(C)DeviceChannel和表(D)Events之间有一对多。表(C)由表(a)设备和(B)通道的2个外键描述。还有一个外键用于描述EventType DeviceChannel没有描述(因此我不能直接将其用作查找事件),尽管它有各种与之关联的值,因此我必须包括设备和通道表 我已经用上面的表构建了一个ViewModel,但是当我尝试创建一个控制器时,我收到错误,抱怨ViewModel没有元数据。表之间没有定义键。所有表都有键,并首先在EF

我正在为Events表构建MVC CRUD页面,在表(C)DeviceChannel和表(D)Events之间有一对多。表(C)由表(a)设备和(B)通道的2个外键描述。还有一个外键用于描述EventType

DeviceChannel没有描述(因此我不能直接将其用作查找事件),尽管它有各种与之关联的值,因此我必须包括设备和通道表

我已经用上面的表构建了一个ViewModel,但是当我尝试创建一个控制器时,我收到错误,抱怨ViewModel没有元数据。表之间没有定义键。所有表都有键,并首先在EF数据库中正确加载

我将VS2013与.Net 4.5.2一起使用 在遵循Nerddinner的示例之后,我确实尝试过使用存储库,但没有成功。 问题: 我是否应该使用ViewModel来开发此解决方案。
如果我使用ViewModel,在尝试折叠控制器时如何解决元数据/密钥问题?

我通过简化ViewModel解决了这个问题

public class DeviceChannelEventsViewModel
{
    public SelectList eventType { get; private set; } 
    public SelectList deviceChannel { get; private set; }
    public CRHSEvent crhsEvent { get; set; }
    public DeviceChannelEventsViewModel(SelectList _deviceChannel, SelectList _eventType, CRHSEvent _crhsEvent)
    {
        deviceChannel = _deviceChannel;
        eventType = _eventType;
        crhsEvent = _crhsEvent;
    }
    public DeviceChannelEventsViewModel(SelectList _deviceChannel, SelectList _eventType)
    {
        deviceChannel = _deviceChannel;
        eventType = _eventType;
        crhsEvent = new CRHSEvent();
    }
}
当我为deviceChannel创建selectlist时,我将设备和频道表加入到deviceChannel表中:

DeviceChannelRepository dcr = new DeviceChannelRepository();
IEnumerable<DeviceChannel> dc = dcr.FindAllDeviceChannelsDescribed(); 
SelectList deviceChannels = new SelectList(dc.Select(s => new 
{ 
    DeviceChannelID = s.DeviceChannelID ,
    DCDescription=string.Format("{0} | {1}",s.Device.DeviceName,s.Channel.Description)
}).ToList(), "DeviceChannelID", "DCDescription");

SelectList eventType = new SelectList(db.EventType, "EventTypeID", "Description");
DeviceChannelEventsViewModel dCEViewModel = 
new DeviceChannelEventsViewModel(deviceChannels, eventType);
DeviceChannel Repository dcr=新的DeviceChannel Repository();
IEnumerable dc=dcr.findallDeviceChannelsDescripted();
选择列表设备频道=新建选择列表
{ 
DeviceChannel ID=s.DeviceChannel ID,
DCDescription=string.Format(“{0}|{1}”,s.Device.DeviceName,s.Channel.Description)
}).ToList(),“DeviceChannel ID”,“DCDescription”);
SelectList eventType=新的SelectList(db.eventType,“EventTypeID”,“Description”);
设备通道事件视图模型dCEViewModel=
新设备通道事件视图模型(设备通道,事件类型);
寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。