VS2012 Entity Framework Error - asp.net-mvc-4

I have a MCV 4.5 solution with 3 projects. Site, Testing and Model. Site and Model are referencing EF 5.0. I have searched all solution files for a reference to 4.3.1 and have come up empty. I have deleted and recreated all references to EF 5.0
I have a HomeControllerTest.cs that runs just fine.
using this as a test
[TestMethod]
public void Index()
{
// Arrange
HomeController controller = new HomeController();
// Act
ViewResult result = controller.Index() as ViewResult;
// Assert
Assert.AreEqual("Modify this template to jump-start your ASP.NET MVC application.", result.ViewBag.Message);
}
I Created a new LOBControllerTest.cs to support the LOBController.cs in the Site Project. This test class fails with the error 'Could not load file or assembly 'EntityFramework, Version=4.3.1.0' using the following test
[TestMethod]
public void Index()
{
// Arrange
LOBController controller = new LOBController();
// Act
ViewResult result = controller.Index() as ViewResult;
// Assert
Assert.IsNotNull(result);
}
If I change the above test to execute the HomeController as in the following, it runs just fine.
[TestMethod]
public void Index()
{
// Arrange
HomeController controller = new HomeController();
// Act
ViewResult result = controller.Index() as ViewResult;
// Assert
Assert.IsNotNull(result);
}
This is a brand new clean install of VS2012 on a clean install of Windows Server 2012.
Any Thoughts?
Update, I forgot to mention I am also using ReSharper 7.1. I'm wondering if that has a setting that I was missing.
I found that there is a documented bug with RS7 stackoverflow.com/questions/12357696/… so I disabled the RS7 testing suite and tried running it directly from VS2012 interface with the same result. So it's not isolated to RS7.
Update:
here's a link https://www.dropbox.com/sh/740w2jsp8i1mslg/pWiwnSewHQ to access this project for anyone who want's to take a look at it. It's nothing special it's just a Template project. We are starting on a new project at work and I'm new to MVC and Test Driven Development so I'm trying to get a head start.
Update 9/21/2012
I believe I've found it. After talking to the Dev who put the original Wholesale template solution together I found that the Repositories folder in the Wholesale.Admin is references a NUGet code package. I checked the package site and found that the latest release is dependent on EF 4.3.1
Update:
This is confirmed. I no longer get the error after downloading and upgrading the solution from the site. I get another error but the 4.3.1 is no longer an issue. Hope the dev will update his NuGet solution, he hasn't updated it in 7 months.

Obviously your LOBController references EF 4.3 somehow, somewhere. Check your references in the class library containing this controller, or maybe a dll that's still in the bin folder or in the GAC...
Hope this helps

After talking to the Dev who put the original Wholesale template solution together I found that the Repositories folder in the Wholesale.Admin references a NUGet code package. I checked the package site and found that the latest release is dependent on EF 4.3.1. I downloaded the source and recompiled referencing EF 5.0 and that cleared the 4.3.1 error

Related

ASP.NET Core Adding View Parameter Name Path Cannot Be Null Error

I'm trying add view to my ASP.NET Core MVC project and I'm getting an error.
The error message is
There was an error running the selected code generator:
'Value cannot be null.
Parameter name:path'
The view is going to use for ViewComponent.
My steps are
Right click ->
Add View ->
Type View Name ->
Uncheck 'Use a Layout Page' ->
Template : Empty ->
Click Add.
I searched about it but can't find any result.
EDIT :
Here is a Startup.cs
namespace Udemy.MvcWebUI
{
public class Startup
{
// This method gets called by the runtime. Use this method to add
//services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IUsersService, UsersManager>();
services.AddScoped<IUsersDAL, EFUsersDAL>();
services.AddScoped<IUsersInfoService, UsersInfoManager>();
services.AddScoped<IUsersInfoDAL, EFUsersInfoDAL>();
services.AddScoped<IEventTypesService, EventTypesManager>();
services.AddScoped<IEventTypesDAL, EFEventTypesDAL>();
services.AddScoped<IEventsService, EventsManager>();
services.AddScoped<IEventsDAL, EFEventsDAL>();
services.AddScoped<ICityService, CityManager>();
services.AddScoped<ICityDAL, EFCityDAL>();
services.AddScoped<IDistrictService, DistrcitManager>();
services.AddScoped<IDistrictDAL, EFDistrictDAL>();
services.AddDbContext<EventContext>(options => options.UseSqlServer(#"Server=localhost\SQLEXPRESS;Database=Events;
User Id=sa;Password=Omurcan.1994;"));
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseFileServer();
app.UseNodeModules(env.ContentRootPath);
app.UseMvcWithDefaultRoute();
// db tanimi icin
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<EventContext>();
context.Database.EnsureCreated();
}
}
}
}
How can I fix it ?
Thanks for your help.
May be Microsoft.VisualStudio.Web.CodeGeneration.Design package is missing in your project!
Install the correct version of Microsoft.VisualStudio.Web.CodeGeneration.Design nuget package according to your project .NET Core version.
Then if you recently updated your project to .NET Core 2.1 from any lower version then update your project's packages to latest version also.
Then don't forget to Download the latest stable version (2.1.500) of .NET Core SDK and install on your machine.
Hope your problem will be solved!
I had a similar issue. It is not working for you because your NuGet package manager is not able to download the Microsoft.VisualStudio.Web.CodeGeneration.Design package. This is happening because you are either not connected to the internet or your NuGet is not configured properly.
You have two options:
1. Configure NuGet:
Tools->NuGet Package Manager->Package Manager Settings
Under NuGet Package Manager dropdown, click Package Sources. Click the green plus to add another source. Name it nuget.org, and define the Source as https://api.nuget.org/v3/index.json
Hit Update and then click OK. Next time you try to add a Razor Page the way you were previously doing (by right clicking Pages->Add->Razor Page), it will automatically download the correct package.
2. Add a Razor Page the traditional way
Right click on Pages->Add->New Item->Razor Page
This way does not required the NuGet package specified above.

Migration targets 2.1.0-rtm-30799 even though Target Framework is .Net Core 2.1

I am using VS2017 version 15.7.3
I am curious as to why a new solution will create a database with migrations listed as being version 2.1.0 but if the identity tables for the very same database are added via the PM Console then the migration will be listed as rtm.
Steps to demonstrate what I mean.
I build a new solution using the ASP.NET Core Web Application template
Choosing Individual User Accounts for Authentication
Then the generated code shows that the data migration will have ProductVersion 2.1.0
namespace WebApplication14.Data.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.1.0")
//etc
I run the application, register a user and click Migrations Applied to create the database.
Next
I use SQL Server Object Explorer to delete all the tables.
I delete the Migrations folder from the solution
In Package Manager Console I type
Add-migration one
Now the ApplicationDbContextModelSnapshot refers to the rtm ProductVersion
namespace WebApplication14.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.1.0-rtm-30799")
If I then run
update-database
the tables get created in the database with the rtm product version listed in __EFMigrationsHistory
I am wondering if this difference is by design or not and if so why it is designed that way.

Use web project config.json when doing integration testing

I am using ASP.NET 5 RC1 and I need to write integration tests ...
So on the Test project, ASPNET5_WEB_TEST, I have the following:
public class IntegrationTests {
private readonly TestServer _server;
private readonly HttpClient _client;
public IntegrationTests() {
_server = new TestServer(TestServer.CreateBuilder().UseStartup<Startup>());
_client = _server.CreateClient();
}
// Test methods ...
}
The Startup class is from the ASP.NET 5 project I am testing: ASPNET5_WEB
When I run the test I get the following error:
The configuration file 'C:\Projects\ASPNET5_TEST\config.json' was not found and is not optional.
I know I get this error because on Startup I have:
builder
.AddJsonFile("config.json", false)
.AddJsonFile($"config.{environment.EnvironmentName}.json", true);
To fix this error I need to copy, at least, config.json from my web project, ASPNET5_WEB, to my test project, ASPNET5_WEB_TEST. But this means I will need to maintain duplicate config.json or at least copy it every time I make a change.
Can't I tell TestServer to use Startup of the web project and also its config.*.json files?
And can I have a config.testing.json and set on the TestServer the environment to Testing so the Startup code uses config.json and config.testing.json?
I assume you're using the TestServer from aspnet, if so, it wasn't built to support the way you're config files are read. The TestServer is used to run simple integration tests for their "hosting engine" but not for integrations tests for a website.
Their ApplicationDeployerFactory class is what you can use however. Refer to this as an example of how to run an "integration" server. I've used selenium in conjunction with that to run integration tests against the project I'm working on atm.
Yes, you can.
Take a look at this issue https://github.com/aspnet/Mvc/issues/3410 and The mentioned package.
Basically you need to implement your own IApplicationEnvironment

Ninject Kernel being disposed on startup after updating Microsoft.AspNet.Identity.Owin 2.2.0

I am working on a WebApplication which uses MVC5 and WebApi 2 with Owin. I recently updated Microsoft Asp.Net NuGet packages (Microsoft.AspNet.Mvc, etc.) from version 5.2.2 to 5.2.3, and the Owin NuGet packages (Microsoft.Owin, etc.) from 3.0.0 to 3.0.1. I also updated Microsoft.AspNet.Identity.Owin from version 2.1.0 to version 2.2.0
I then updated the corresponding Ninject WebApi packages (Ninject.Web.WebApi, etc.) from 3.2.3 to version 3.2.4 in order to get it to compile, but did not update Ninject.Web.Common.OwinHost, since this was at the latest version (3.2.3).
When I try to run the application, I get the following error:
Error loading Ninject component ICache
No such component has been registered in the kernel's component container.
Suggestions:
1) If you have created a custom subclass for KernelBase, ensure that you have properly implemented the AddComponents() method.
2) Ensure that you have not removed the component from the container via a call to RemoveAll().
3) Ensure you have not accidentally created more than one kernel.
The Kernel that I am creating in the OwinStartup class using is being disposed from the Owin.AppBuilderExtensions.CreateOwinContext() method, which is indirectly from OwinBootstrapper.Execute().
This has only started happening since updating the Asp.Net NuGet packages to 5.2.3. Before updating the packages, OwinBootstrapper.Execute() is still called, but does not cause Owin.AppBuilderExtensions.CreateOwinContext() or KernelBase.Dispose() to be called.
I have not changed any of the code in OwinStartup, and my Ninject Kernel is still being created using:
public virtual void Configuration(IAppBuilder app)
{
app.UseNinjectMiddleware(CreateKernel);
app.CreatePerOwinContext(CreateKernel);
}
I have tried updating the NuGet packages one at a time, and the specific update that causes the issue is Microsoft.AspNet.Identity.Owin to 2.2.0 Are there any known compatibility issues with Ninject and AspNet.Identity.Owin 2.2.0?
This from Hao: I think that should be fine, so long as ninject takes care of disposing the per request objects somehow
By looking at the previous source code and current source code, it looks like they are expecting a IDisposable object and calls it immediately at the end of its life cycle (aka request).
I also noticed that other CreatePerOwinContext they provide when installing OWIN such as app.CreatePerOwinContext(ApplicationDbContext.Create); was never disposed (in 2.1.0)? This seems like a big memory leak as they instantiate some classes every time there is a request.
To go around the problem when using CreatePerOwinContext with Ninject StandardKernel, I tried with the following code:
app.CreatePerOwinContext(
(Microsoft.AspNet.Identity.Owin.IdentityFactoryOptions<IKernel> options, IOwinContext context) => kernel,
(Microsoft.AspNet.Identity.Owin.IdentityFactoryOptions<IKernel> options, IKernel instance) => {}
);
Basically, I do nothing in the dispose callback.
I do not know if this will lead to some memory leak but it definitely makes the app work again.

Unity.MVC4 Doesn't Dispose()

I've installed the NuGet Unity.MVC4 package http://nuget.org/packages/Unity.Mvc4/ and have hooked it up the same as I would if I were using the Unity.MVC3 package.
My dependencies are getting resolved correctly, but when using the HierarchicalLifetimeManager, my object's Dispose() method is not called at the end of the request.
I've actually set up a simple project using Unity.MVC3 and Unity.MVC4, with the exact same Bootstrapper.cs code in both projects. On the MVC3 version, Dispose() is called; on the MVC4 version, it is not.
I'm registering my type like so
container.RegisterType<ITest, Test>(new HierarchicalLifetimeManager());
with a very simple Test class
public interface ITest
{
void Foo();
}
public class Test : IDisposable, ITest
{
public Test()
{
}
public void Foo()
{
}
public void Dispose()
{
}
}
I don't think I'm doing anything incorrectly, but there appears to be some bug in the Unity.MVC4 package.
If anyone is successfully using that package, I'm curious how you got it to work.
The best solution to this issue is to upgrade to Unity3 and install the Unity Bootstrapper for ASP.NET MVC package. The Bootstrapper package includes PerRequestLifetimeManager which should be used in place of HierarchicalLifetimeManager. Once the integration is complete, Dispose() is called at the end of every HTTP request just as expected.
Note that the newest Unity documentation suggests that PerRequestLifetimeManager should not be used at all unless absolutely necessary. (I believe, however, that it is still needed in some cases - for example, where you want to share a DbContext across all objects used during a single request.)
With this solution, Unity.MVC4 is no longer needed and can be uninstalled.
I have had a look at the code. We simply ported the Unity.MVC3 code from codeplex (http://unitymvc3.codeplex.com/) and re-bundled it up for MVC4.
We have not made any alterations to the code.
Unity.MVC4 makes use of the Unity package managed by Microsoft (http://nuget.org/packages/Unity/). The majority of the code is in Unity.
Are you running different Dot.Net versions? i.e 4.0 vs 4.5? There seems to be a pre-release Unity for DotNet 4.5.