Abdulmajeed Alshari

Abdulmajeed Alshari

  • NA
  • 266
  • 61.4k

Entity Framework code first. Can't add migration

Sep 10 2019 4:03 AM
when write 
PM> add-migration createDatabase 
 
appear this error message :
 
System.TypeInitializationException: The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception. ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element. (C:\Users\Eng. Abdulmajeed\documents\visual studio 2015\Projects\Housing_System\Housing_System\tmp7E06.tmp line 10)
at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
--- End of inner exception stack trace ---
at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
at System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String sectionName)
at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
at System.Configuration.ConfigurationManager.get_ConnectionStrings()
at System.Data.Entity.Internal.AppConfig..ctor()
at System.Data.Entity.Internal.AppConfig..cctor()
--- End of inner exception stack trace ---
at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.EnsureLoadedForAssembly(Assembly assemblyHint, Type contextTypeHint)
at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.EnsureLoadedForContext(Type contextType)
at System.Data.Entity.Migrations.DbMigrationsConfiguration`1..cctor()
--- End of inner exception stack trace ---
at System.Data.Entity.Migrations.DbMigrationsConfiguration`1..ctor()
at Housing_System.Migrations.Configuration..ctor() in C:\Users\Eng. Abdulmajeed\documents\visual studio 2015\Projects\Housing_System\Housing_System\Migrations\Configuration.cs:line 10
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Data.Entity.Migrations.Utilities.MigrationsConfigurationFinder.FindMigrationsConfiguration(Type contextType, String configurationTypeName, Func`2 noType, Func`3 multipleTypes, Func`3 noTypeWithName, Func`3 multipleTypesWithName)
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.FindConfiguration()
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' threw an exception.
 
 
web.config as the follow:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--  
  3.   For more information on how to configure your ASP.NET application, please visit  
  4.   http://go.microsoft.com/fwlink/?LinkId=301880  
  5.   -->  
  6. <configuration>  
  7.    <connectionStrings>  
  8.     <add name="DBConnection" connectionString="Data Source=.;Integrated Security=True;initial catalog=Housing_Database;" providerName="System.Data.SqlClient" />  
  9.   </connectionStrings>  
  10.   <configSections>  
  11.     <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->  
  12.     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />  
  13.   </configSections>  
  14.    
  15.   <appSettings>  
  16.     <add key="webpages:Version" value="3.0.0.0" />  
  17.     <add key="webpages:Enabled" value="false" />  
  18.     <add key="ClientValidationEnabled" value="true" />  
  19.     <add key="UnobtrusiveJavaScriptEnabled" value="true" />  
  20.   </appSettings>  
  21.   
  22.   <system.web>  
  23.     <compilation debug="true" targetFramework="4.5.2" />  
  24.     <httpRuntime />  
  25.     <httpModules>  
  26.       <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />  
  27.     </httpModules>  
  28.     <pages controlRenderingCompatibilityVersion="4.0" />  
  29.   </system.web>  
  30.   <runtime>  
  31.     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
  32.       <dependentAssembly>  
  33.         <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />  
  34.         <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />  
  35.       </dependentAssembly>  
  36.       <dependentAssembly>  
  37.         <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />  
  38.         <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />  
  39.       </dependentAssembly>  
  40.       <dependentAssembly>  
  41.         <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />  
  42.         <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />  
  43.       </dependentAssembly>  
  44.       <dependentAssembly>  
  45.         <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />  
  46.         <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />  
  47.       </dependentAssembly>  
  48.       <dependentAssembly>  
  49.         <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />  
  50.         <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />  
  51.       </dependentAssembly>  
  52.       <dependentAssembly>  
  53.         <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />  
  54.         <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />  
  55.       </dependentAssembly>  
  56.     </assemblyBinding>  
  57.   </runtime>  
  58.   <system.codedom>  
  59.     <compilers>  
  60.       <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />  
  61.       <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />  
  62.     </compilers>  
  63.   </system.codedom>  
  64.   <system.webServer>  
  65.     <validation validateIntegratedModeConfiguration="false" />  
  66.     <modules>  
  67.       <remove name="ApplicationInsightsWebTracking" />  
  68.       <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />  
  69.     </modules>  
  70.   </system.webServer>  
  71.   <entityFramework>  
  72.     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">  
  73.       <parameters>  
  74.         <parameter value="mssqllocaldb" />  
  75.       </parameters>  
  76.     </defaultConnectionFactory>  
  77.     <providers>  
  78.       <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />  
  79.     </providers>  
  80.   </entityFramework>  
  81. </configuration>  
 
 IdentityModels.cs
 
  1. using System.Data.Entity;  
  2. using System.Security.Claims;  
  3. using System.Threading.Tasks;  
  4. using Microsoft.AspNet.Identity;  
  5. using Microsoft.AspNet.Identity.EntityFramework;  
  6.   
  7. namespace Housing_System.Models  
  8. {  
  9.     // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.  
  10.     public class ApplicationUser : IdentityUser  
  11.     {         
  12.         public int CardNo { getset; }  
  13.         public string CardType { getset; }  
  14.         public string FullName { getset; }  
  15.   
  16.         public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)  
  17.         {  
  18.             // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType  
  19.             var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);  
  20.             // Add custom user claims here  
  21.             return userIdentity;  
  22.         }  
  23.     }  
  24.   
  25.     public class ApplicationDbContext : IdentityDbContext<ApplicationUser>  
  26.     {  
  27.         public ApplicationDbContext()  
  28.             : base("DBConnection", throwIfV1Schema: false)  
  29.         {  
  30.         }  
  31.   
  32.         public static ApplicationDbContext Create()  
  33.         {  
  34.             return new ApplicationDbContext();  
  35.         }  
  36.         public DbSet<Bed> Beds { getset; }  
  37.         public DbSet<Employee> Employees { getset; }  
  38.         public DbSet<Building> Buildings { getset; }  
  39.         public DbSet<Flat> Flats { getset; }  
  40.         public DbSet<Floor> Floors { getset; }  
  41.         public DbSet<Gust> Gusts { getset; }  
  42.         public DbSet<Room> Rooms { getset; }  
  43.         public DbSet<student> students { getset; }  
  44.         public DbSet<Subscriper> Subscripers { getset; }  
  45.         public DbSet<Subscription> Subscriptions { getset; }  
  46.         public DbSet<Residence> residences { getset; }  
  47.         public DbSet<Payment> payments { getset; }  
  48.   
  49.     }  
  50. }  
 
 

Answers (2)