Asp.net Core EF options.UseInMemoryDatabase System.TypeLoadException - asp.net-core

I used EF in Asp.net Core, but got below error in below code:
public class TodoContext : DbContext
{
public TodoContext(DbContextOptions<TodoContext> options)
: base(options)
{
}
public DbSet<TodoItem> TodoItems { get; set; }
}
Error Message:
An exception of type 'System.TypeLoadException' occurred in
Microsoft.EntityFrameworkCore.dll but was not handled in user code
Additional information: Could not load type
'Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionExtensions'
from assembly 'Microsoft.Extensions.DependencyInjection.Abstractions,
Version=1.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
Here is my Project.json
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.Extensions.Logging.Console": "1.0.0",
//Dependence for MVC
"Microsoft.AspNetCore.Mvc": "1.1.1",
"Microsoft.AspNetCore.StaticFiles": "1.1.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
"Microsoft.Extensions.Configuration.Json": "1.1.0",
//Dependence for EF
"Microsoft.EntityFrameworkCore": "1.1.0",
"Microsoft.EntityFrameworkCore.InMemory": "1.0.0-rc2-final"
//Dependence for EF with SQL, this is avalible under VS 2017 RC
//"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
//Entity Framework commands to maintain the database
//"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview4-final"
},
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
//used for Razor pages which are compiled at runtime,and the compiler needs access to reference assemblies,
//to make sure it compiles correctly
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"wwwroot",
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
Any help would be appreciated.
Reards,
Edward

After trying, I changed "1.0.0-rc2-final" to "1.1.0" which I already have tried, but I found there is an warning, "Dependency conflict. CoreMVCWebAPI 1.0.0 expected Microsoft.EntityFrameworkCore.InMemory >= 1.1.0 but received 1.0.0-rc2-final", after install this package manually instead of only changing project.json, it works now.
Install-Package Microsoft.EntityFrameworkCore.InMemory

I just wanted to add that I had to restart VS2017 before the package restore was recognised. Prior to that I was getting an error
DbContextOptionsBuilder does not contain a definition for
UseInMemoryDatabase() ...
I hope this saves you time.

Related

Referencing .net 4.6.1 Library in .net Core

I am creating .net core WebApi and trying to use a library created previously using .net 4.6.1. I have created a NuGet package for that 4.6.1 library and able to add that to my NET Core application without any issue. But when I reference that library in my .netcore controller, I get message "you can use Navigation bar to switch context".
Here is my project.json:
{
"dependencies": {
"Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0"
},
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"net461": {
"dependencies": {
"Db.Library": "1.0.0"
}
},
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
],
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.1"
}
}
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"wwwroot",
"**/*.cshtml",
"appsettings.json",
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
there is no "dnxcore50": { } used.
So, cannot remove that as suggested in other threads. What could be the solution?
Update:
The following link help me a lot :
http://www.hanselman.com/blog/HowToReferenceAnExistingNETFrameworkProjectInAnASPNETCore10WebApp.aspx
Now I created a new project with .netFramework and added my nuget package and able to compile.
Posting this, if it helps someone.

Could not load file or assembly mscorlib

I'm new in NetCore and I try to load a dll dynamically with this code :
public void LoadDll()
{
try
{
var dllPath = #"C:\Temp\dynamic.dll";
var asl = new AssemblyLoader();
var asm = asl.LoadFromAssemblyPath(dllPath);
var type = asm.GetType("MyNamespace.MyClass");
dynamicInstance = Activator.CreateInstance(type);
}
catch (Exception ex)
{
}
}
public class AssemblyLoader : AssemblyLoadContext
{
// Not exactly sure about this
protected override Assembly Load(AssemblyName assemblyName)
{
var deps = DependencyContext.Default;
var res = deps.CompileLibraries.Where(d => d.Name.Contains(assemblyName.Name)).ToList();
var assembly = Assembly.Load(new AssemblyName(res.First().Name));
return assembly;
}
}
When I call asm.GetType method a exception is thrown :
"Could not load file or assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. An operation is not legal in the current state. (Exception from HRESULT: 0x80131509)" Source : System.Private.CoreLib
Here is my project.json :
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
"Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0"
},
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"wwwroot",
"**/*.cshtml",
"appsettings.json",
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
Can someone help me ?
SThis is a very common problem. As of .NET Core 1.1 you cannot load libraries built for the .NET Framework (they are mscorlib based) but only ones which are build using PCL.
Check the assembly you try to load.
.NET Core 2.0 will address this topic.

Error when scaffolding using Entity Framework Core in ASP.Net core

I am trying to create models using an existing SQL Server 2016 database. When the run the command prompt as shown in the documentation:
PM> Scaffold-DbContext "'Server=techestatevm.southeastasia.cloudapp.azure.com;Database=Blogging;User Id=newuser;Password=pass;'" Microsoft.EntityFrameworkCore.SqlServer -verbose
I get this error:
Working directory: E:\Personal\TE\VS Projects\EFGetStarted.AspNetCore.ExistingDb\src\EFGetStarted.AspNetCore.ExistingDb
Executing command: dotnet ef --configuration Debug --build-base-path .\bin\ dbcontext scaffold ''Server=techestatevm.southeastasia.cloudapp.azure.com;Database=Blogging;User Id=newuser;Password=pass;''
Microsoft.EntityFrameworkCore.SqlServer --verbose
At line:1 char:215
+ ... user;Password=pass;'' Microsoft.EntityFrameworkCore.SqlServer --verbo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'Microsoft.EntityFrameworkCore.SqlServer' in expression or statement.
I have added all the Nuget packages. See my project.json :
{
"dependencies": {
"Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
"Microsoft.EntityFrameworkCore": "1.0.0-rc2-final",
"Microsoft.EntityFrameworkCore.InMemory": "1.0.0-rc2-final",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final",
"Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0-rc2-final",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview1-final",
"Microsoft.EntityFrameworkCore.Tools.Cli": "1.0.0-preview1-final",
"Microsoft.EntityFrameworkCore.Tools.Core": "1.0.0-rc2-final",
"Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore": "1.0.0-preview1-final"
},
"tools": {
"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-preview1-final",
"imports": [
"portable-net45+win8+dnxcore50",
"portable-net45+win8"
]
},
"Microsoft.AspNetCore.Server.IISIntegration.Tools": {
"version": "1.0.0-preview1-final",
"imports": "portable-net45+win8+dnxcore50"
}
},
"frameworks": {
"net452": { }
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"publishOptions": {
"include": [
"wwwroot",
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
Looks like this is a documentation issue. The suggestion caution as mentioned in the doc is the culprit. Removing the outer double quotes fixed the issue.
Note that the connection string is double quoted (with single quotes inside the double quotes). This is a workaround for a known issue in RC2 for more details.

You must add a reference to assembly 'mscorlib, Version=4.0.0.0 in Mac

I'm trying to compile a ASP.NET Core RC2 Project in a Mac with ASP.NET Core RC2 installed. This is my project.json:
{
"userSecretsId": "aspnet-TestCoreRC2-6a20d1d3-f55a-4683-a8ca-5f5fbff19ab4",
"dependencies": {
"Microsoft.AspNetCore.Authentication.Cookies": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview1-final",
"type": "build"
},
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
"Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
"Microsoft.EntityFrameworkCore.Sqlite": "1.0.0-rc2-final",
"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-preview1-final",
"type": "build"
},
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-final",
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
"version": "1.0.0-preview1-final",
"type": "build"
},
"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
"version": "1.0.0-preview1-final",
"type": "build"
}
},
"tools": {
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview1-final",
"imports": "portable-net45+win8+dnxcore50"
},
"Microsoft.AspNetCore.Server.IISIntegration.Tools": {
"version": "1.0.0-preview1-final",
"imports": "portable-net45+win8+dnxcore50"
},
"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-preview1-final",
"imports": [
"portable-net45+win8+dnxcore50",
"portable-net45+win8"
]
},
"Microsoft.Extensions.SecretManager.Tools": {
"version": "1.0.0-preview1-final",
"imports": "portable-net45+win8+dnxcore50"
},
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
"version": "1.0.0-preview1-final",
"imports": [
"portable-net45+win8+dnxcore50",
"portable-net45+win8"
]
}
},
"frameworks": {
"net451": {
"dependencies": {
"System.Runtime": "4.0.20"
}
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"gcServer": true
},
"publishOptions": {
"include": [
"wwwroot",
"Views",
"appsettings.json",
"web.config"
]
},
"scripts": {
"prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
},
"tooling": {
"defaultNamespace": "TestCoreRC2"
}
}
This is the error:
An error occurred during the compilation of a resource required to
process this request. Please review the following specific error
details and modify your source code appropriately.
Generated Code
Cannot define a class or member that utilizes 'dynamic' because the
compiler required type
'System.Runtime.CompilerServices.DynamicAttribute' cannot be found.
Are you missing a reference?
public class _Views_Home_Index_cshtml : Microsoft.AspNetCore.Mvc.Razor.RazorPage Predefined type
'System.Boolean' is not defined or imported
public class _Views_Home_Index_cshtml : Microsoft.AspNetCore.Mvc.Razor.RazorPage The type 'Object' is
defined in an assembly that is not referenced. You must add a
reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089'.
Thanks
Have a look here
Asp.Net Core currently does not work well under Mono net451, but there is a chance that it might work.
Just follow the workaround instructions in the link provided.
I was also facing similar issues of referencing mscorlib. Resolved it by adding "Microsoft.NETCore.Portable.Compatibility": "1.0.1-rc2-24027". This package resolves dependencies with PCL still targeting mscorlib. More details are here
Edit
Just now I saw you are targeting net451 on Mac. You will need to switch to "netcoreapp1.0": {} to support cross platform. Try making following changes to project.json:
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
},
//other dependencies
},
"frameworks": {
"netcoreapp1.0": {}
},
//Other configs

Reference a NET461 DLL model type from ASP.NET Core View

I have a new ASP.NET Core RC2 built as follows:
Model stored in DLL library of .NET Framework 461
Web is MVC Core 1.0 Full Framework - not based on core framework
when reference the model type in the DLL library and run the project, i got the following error:
An error occurred during the compilation of a resource required to
process this request. Please review the following specific error
details and modify your source code appropriately. Generated Code The
type or namespace name 'Data' does not exist in the namespace
'ADMA.EWRS' (are you missing an assembly reference?)
23. public class _Views_Murad_Index_cshtml : Microsoft.AspNetCore.Mvc.Razor.RazorPage>
The type or namespace name 'Data' does not exist in the namespace
'ADMA.EWRS' (are you missing an assembly reference?)
39. public Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper>
Html { get; private set; } An error occurred during the compilation of
a resource required to process this request. Please review the
following specific error details and modify your source code
appropriately. Generated Code The type or namespace name 'Data' does
not exist in the namespace 'ADMA.EWRS' (are you missing an assembly
reference?)
23. public class _Views_Murad_Index_cshtml : Microsoft.AspNetCore.Mvc.Razor.RazorPage>
The type or namespace name 'Data' does not exist in the namespace
'ADMA.EWRS' (are you missing an assembly reference?)
39. public Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper>
Html { get; private set; }
Config Project.json as follow :
{
"dependencies": {
"Microsoft.ApplicationInsights.AspNetCore": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview1-final",
"type": "build"
},
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
"Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-final",
"Microsoft.AspNetCore.Authorization": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Authentication": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Authentication.Cookies": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Session": "1.0.0-rc2-final",
"ADMA.EWRS.Web.Security": "1.0.0-*"
},
"tools": {
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview1-final",
"imports": "portable-net45+win8+dnxcore50"
},
"Microsoft.AspNetCore.Server.IISIntegration.Tools": {
"version": "1.0.0-preview1-final",
"imports": "portable-net45+win8+dnxcore50"
}
},
"frameworks": {
"net461": {
"dependencies": {
"ADMA.EWRS.BizDomain": {
"target": "project"
},
"ADMA.EWRS.Data.Models": {
"target": "project"
}
}
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"publishOptions": {
"include": [
"wwwroot",
"Views",
"appsettings.json",
"web.config"
]
},
"scripts": {
"prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
Solution Explorer
X Project
Solved after Adding Razor Options as in below code :
services.AddMvc().// Murad Add this for RC2, remove it if release 1.0 after June
AddRazorOptions(options =>
{
var previous = options.CompilationCallback;
options.CompilationCallback = context =>
{
previous?.Invoke(context);
context.Compilation = context.Compilation.AddReferences(Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(ADMA.EWRS.Data.Models.Murad).Assembly.Location));
};
});
//var myAssemblies = AppDomain.CurrentDomain.GetAssemblies().Select(x => Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(x.Location)).ToList();
//services.Configure((Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions options) =>
//{
// var previous = options.CompilationCallback;
// options.CompilationCallback = (context) =>
// {
// previous?.Invoke(context);
// context.Compilation = context.Compilation.AddReferences(myAssemblies);
// };
//});
Check
https://github.com/aspnet/Mvc/issues/4686