Application can't scaffold items

后端 未结 29 1258
自闭症患者
自闭症患者 2020-11-30 01:57

I created an MVC 5 application in VS 2013 Professional and then used EF 6.1 code first with an existing DB on SQL Server Express. When I try to create the views I’m using th

29条回答
  •  萌比男神i
    2020-11-30 02:26

    Your context class might be throwing an exception.

    I was following along in the book "C# 6.0 and the .NET 4.6 Framework" and one of the last exercises of the data access layer section was to add logging. Well, I guess that blows up the scaffold wizard. Probably file permissions on sqllog.txt or HttpRuntime is not defined...who knows. When I commented all this stuff out, it worked again.

    namespace AutoLotDAL.EF
    {
        using System.Data.Entity;
        using System.Data.Entity.Infrastructure;
        using System.Data.Entity.Infrastructure.Interception;
        using AutoLotDAL.Interception;
        using AutoLotDAL.Models;
        using System;
        using System.Data.Entity.Core.Objects;
        using System.Web;
    
        public class AutoLotEntities : DbContext
        {
            //static readonly DatabaseLogger databaseLogger =
            //    new DatabaseLogger($"{HttpRuntime.AppDomainAppPath}/sqllog.txt", true);
    
            public AutoLotEntities()
                : base("name=AutoLotConnection")
            {
                ////DbInterception.Add(new ConsoleWriterInterceptor());
                //databaseLogger.StartLogging();
                //DbInterception.Add(databaseLogger);
    
                //// Interceptor code
                //var context = (this as IObjectContextAdapter).ObjectContext;
                //context.ObjectMaterialized += OnObjectMaterialized;
                //context.SavingChanges += OnSavingChanges;
            }
    
            //void OnObjectMaterialized(object sender,
            //    System.Data.Entity.Core.Objects.ObjectMaterializedEventArgs e)
            //{
    
            //}
    
            //void OnSavingChanges(object sender, EventArgs eventArgs)
            //{
            //    // Sender is of type ObjectContext.  Can get current and original values,
            //    // and cancel/modify the save operation as desired.
            //    var context = sender as ObjectContext;
            //    if (context == null)
            //        return;
            //    foreach (ObjectStateEntry item in
            //        context.ObjectStateManager.GetObjectStateEntries(
            //            EntityState.Modified | EntityState.Added))
            //    {
            //        // Do something important here
            //        if ((item.Entity as Inventory) != null)
            //        {
            //            var entity = (Inventory)item.Entity;
            //            if (entity.Color == "Red")
            //            {
            //                item.RejectPropertyChanges(nameof(entity.Color));
            //            }
            //        }
            //    }
            //}
    
            //protected override void Dispose(bool disposing)
            //{
            //    DbInterception.Remove(databaseLogger);
            //    databaseLogger.StopLogging();
            //    base.Dispose(disposing);
            //}
    
            public virtual DbSet CreditRisks { get; set; }
            public virtual DbSet Customers { get; set; }
            public virtual DbSet Inventory { get; set; }
            public virtual DbSet Orders { get; set; }
        }
    }
    

提交回复
热议问题