ASP.NET MVC/EF Code First error: Unable to retrieve metadata for model

橙三吉。 提交于 2020-02-29 04:28:31

问题


I'm trying to create a controller in MVC4 and I'm getting an error I don't understand (I'm new to MVC). It says "Unable to retrieve metadata for 'CIT.ViewModels.DashboardViewModel'..." and then gives 2 possible problems. One is that the DashboardViewModel has no key defined. The other is that EntitySet 'DashboardViewModels' has no key defined.

I defined a key for DashboardViewModel, but that didn't solve the problem. Here is my DashboardViewModel;

public class DashboardViewModel
    {

        public DashboardViewModel() { }

        [Key]
        public int Id { get; set; }
        public Hardware Hardware { get; set; }
        public Software Software { get; set; }
        public HardwareType HardwareType { get; set; }
        public Manufacturer Manufacturer { get; set; }
        public SoftwarePublisher SoftwarePublisher { get; set; }


    }

As you can see it is composed of classes. I did this so I could have multiple classes accessible from the same view. I didn't think it needed a key, but I added one and that didn't fix the problem. The other error sounded like it was looking for a DbSet for DashboardViewModels. As I understand it, your DbSets are your tables. I don't want or need a DashboardViewModels table. I'm only doing that so I can have multiple tables/classes accessible in my view. That's working fine up to this point.

When I am trying to create the controller, I am using the DashboardViewModel as as my model and Context as my context. Here is my context:

public class Context : DbContext
    {
        public DbSet<Software> Softwares { get; set; }
        public DbSet<Location> Locations { get; set; }
        public DbSet<SoftwarePublisher> SoftwarePublishers { get; set; }
        public DbSet<SoftwareType> SoftwareTypes { get; set; }
        public DbSet<Hardware> Hardwares { get; set; }
        public DbSet<Manufacturer> Manufacturers { get; set; }
        public DbSet<HardwareType> HardwareTypes { get; set; }
 }

How do I address these errors?

来源:https://stackoverflow.com/questions/14125892/asp-net-mvc-ef-code-first-error-unable-to-retrieve-metadata-for-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!