Cannot create controller with Entity framework - Unable to retrieve metadata

前端 未结 7 1966
慢半拍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<HMContext>(null);
            }
    
            /// ... ///
    
            public virtual DbSet<Medicine> Medicines { get; set; }
            public virtual DbSet<MedicineGeneric> MedicineGenerics { get; set; }
            public virtual DbSet<MedicineStrength> MedicineStrengths { get; set; }
            public virtual DbSet<MedicineType> MedicineTypes { get; set; }
            public virtual DbSet<MedicineDosage> MedicineDosages { get; set; }
            public virtual DbSet<MedicineCategory> MedicineCategories { get; set; }
            public virtual DbSet<MetricPrefix> MetricPrefixes { get; set; }
            public virtual DbSet<Unit> Units { get; set; }
    
            ///...///
        } 
    
    0 讨论(0)
提交回复
热议问题