Cannot create controller with Entity framework - Unable to retrieve metadata

前端 未结 7 1973
慢半拍i
慢半拍i 2021-02-12 20:35

I\'m using VS2013

When I try to create an \"MVC 5 Controller with views using entity Framework\" I get the following error:

the         


        
7条回答
  •  渐次进展
    2021-02-12 21:42

    Please check all the Models are registered in context

    Example:

    You have a Model like this

    public class MedicineStrength
    {
        [Key]
        public int Id { get; set; }
        public int? MedicineId { get; set; }
        [ForeignKey("MedicineId")]
        public Medicine Medicine { get; set; }
    
        public double Strength { get; set; }
    
        public int? MetricPrefixId { get; set; }
        [ForeignKey("MetricPrefixId")]
        public virtual MetricPrefix MetricPrefix { get; set; }
    
        public int? UnitId { get; set; }
        [ForeignKey("UnitId")]
        public virtual Unit Unit { get; set; }
    
        public int? MedicineTypeId { get; set; }
        [ForeignKey("MedicineTypeId")]
        public virtual MedicineType MedicineType { get; set; }
    }
    

    Check all virtual Model instances are registered in in context like this

    public class HMContext : DbContext
        {
            public HMContext()
            : base("name=HMContext")
            {
                Database.SetInitializer(null);
            }
    
            /// ... ///
    
            public virtual DbSet Medicines { get; set; }
            public virtual DbSet MedicineGenerics { get; set; }
            public virtual DbSet MedicineStrengths { get; set; }
            public virtual DbSet MedicineTypes { get; set; }
            public virtual DbSet MedicineDosages { get; set; }
            public virtual DbSet MedicineCategories { get; set; }
            public virtual DbSet MetricPrefixes { get; set; }
            public virtual DbSet Units { get; set; }
    
            ///...///
        } 
    

提交回复
热议问题