Entity Framework added to references but DbContext is not found - c#

I added Entity Framework to references but I still get an error that it does not exist .

I think this issue is from the Pluralsight video: Building an Enterprise App with WPF, MVVM, and Entity Framework Code First.
At Section 5, in the area titled: Create a DbContext Subclass, the author (T. Huber) - installs the Entity Framework 6.1.3 (not the core version) - from NuGet. When I did this, I grabbed version 6.4.4. After doing that, using System.Data.Entity; did not work. I would get:
"CS0234: The type or namespace 'Entity' does not exist in he namespace 'System.Data' (are you missing an assembly reference?)" Later I tried EF Core, only to find the base("FriendOrganizerDb") doesn't like that string in there.
My solution was to uninstall the Nuget packages, and Install NuGet Entity Framework ver 6.1.3 as per the video. That "version" of Entity Framework works with: using System.Data.Entity;

We have very little information about your project settings, but it seems that you are using .Net Core. In order to use EntityFramework in .Net Core you need to follow these steps:
.Net Core:
You need to install Microsoft.EntityFrameworkCore package instead of EntityFramework. You can install this package via nuget using: Install-Package Microsoft.EntityFrameworkCore -Version 3.1.7 (You might replace 3.1.7 with other versions if needed)
After installing the package you need to add using Microsoft.EntityFrameworkCore; to you usings list or just replcae DbContext with Microsoft.EntityFrameworkCore.DbContext
using Microsoft.EntityFrameworkCore;
namespace FriendOrganizer.DataAccess{
public class FriendOrganizerDbContext: DbContext{
...
}
}
Or
namespace FriendOrganizer.DataAccess{
public class FriendOrganizerDbContext: Microsoft.EntityFrameworkCore.DbContext{
...
}
}
However if you want to use ENtityFramework in .Net Framework you should follow these steps instead:
.Net Framework
You need to install EntityFramework package. You can install it via nuget using: Install-Package EntityFramework -Version 6.4.4 (You might replace 6.4.4 with other versions if needed),
After installing the package you need to add System.Data.Entity to your usings list or just replace the DbContext with System.Data.Entity.DbContext
using System.Data.Entity;
namespace FriendOrganizer.DataAccess{
public class FriendOrganizerDbContext: DbContext{
...
}
}
Or
namespace FriendOrganizer.DataAccess{
public class FriendOrganizerDbContext: System.Data.Entity.DbContext{
...
}
}

Related

How to configure EF connection string from .NET Standard library? No UseSqlServer in DbContextOptionsBuilder? [duplicate]

I am new to EF core and I'm trying to get it to work with my ASP.NET Core project.
I get the above error in my startup.cs when trying configure the DbContext to use a connection string from config. I am following this tutorial.
The problematic code is in startup.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using tracV2.models;
using tracV2.data;
namespace tracV2
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddSingleton<IConfiguration>(Configuration);
string conn = Configuration.GetConnectionString("optimumDB");
services.AddDbContext<tracContext>(options => options.usesqlserver(conn));
}
The UseSqlServer method is recognized if I put it directly into the context:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace tracV2.data
{
public class tracContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("myrealconnectionstring");
}
All my research online points to missing references, but I can't seem to find out which one I am missing (see image).
First we install the Microsoft.EntityFrameworkCore.SqlServer NuGet Package:
PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer
Then, after importing the namespace with
using Microsoft.EntityFrameworkCore;
we add the database context:
services.AddDbContext<AspDbContext>(options =>
options.UseSqlServer(config.GetConnectionString("optimumDB")));
adding
using Microsoft.EntityFrameworkCore;
manually solved the problem for me
Found that here
Edit...
for dotnet core 3.1 add
Microsoft.EntityFrameworkCore.SqlServer
Follow the steps below.
Install Entity Framework Core Design and SQL Server database provider for Entity Framework Core:
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
Import Entity Framework Core:
using Microsoft.EntityFrameworkCore;
And configure your DbContext:
var connectionString = Configuration.GetConnectionString("myDb");
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(connectionString)
);
Install below NuGet Package will solve your issue
Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.SqlServer
EntityFramework UseSqlServer Solved
Install-Package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer
I believe this can be solved by adding a project reference to Microsoft.EntityFrameworkCore.SqlServer.Design
Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design
Microsoft.EntityFrameworkCore.SqlServer wasn't directly installed in my project, but the .Design package will install it anyway as a prerequisite.
The Package is missing. Open Package Manager Console and execute the code below:
Install-Package Microsoft.EntityFrameworkCore.SqlServer
your solution works great.
When I saw this video till 17 Minute: https://www.youtube.com/watch?v=fom80TujpYQ
I was facing a problem here:
services.AddDbContext<PaymentDetailContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DevConnection")));
UseSqlServer not recognizes so I did this
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.5
&
using Microsoft.EntityFrameworkCore;
Then my problem is solved. About me: basically I am a purely PHP programmer since beginning and today only I started .net coding, thanks for good community in .net
Install the following packages from Nuget :-
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Sqlite.Core
I was using Visual Studio Code.
1) Try to install the package 'Microsoft.EntityFrameworkCore.SqlServer' by specifying the version number.
VS Code:
'dotnet add package Microsoft.EntityFrameworkCore.SqlServer -v 1.1.1'
Visual Studio:-
'Install-Package Microsoft.EntityFrameworkCore.SqlServer -v 1.1.1'
Refer the link 'Package 'Microsoft.EntityFrameworkCore.SqlServer' is incompatible with 'all' frameworks in the project' for doing it.
2) Then add the namespace 'using Microsoft.EntityFrameworkCore;' manually in the Startup.cs file.
Refer the below link
https://github.com/aspnet/EntityFramework/issues/7891.
3) If you get any dependency issue for 'Microsoft.EntityFrameworkCore.SqlServer.Design', like "Package 'Microsoft.EntityFrameworkCore.Design' is incompatible with 'all' frameworks in project" ,we need to run the below command,
VS Code:-
dotnet add package Microsoft.EntityFrameworkCore.Design -v 1.1
Visual Studio
Install-Package Microsoft.EntityFrameworkCore.Design -v 1.1
Project -> ManageNugetPackages -> Browse -> Search "Microsoft.EntityFrameworkCore.SqlServer" and install or update.
As mentioned by top scoring answer by Win you may need to install Microsoft.EntityFrameworkCore.SqlServer NuGet Package, but please note that this question is using asp.net core mvc. In the latest ASP.NET Core 2.1, MS have included what is called a metapackage called Microsoft.AspNetCore.App
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/metapackage-app?view=aspnetcore-2.2
You can see the reference to it if you right-click the ASP.NET Core MVC project in the solution explorer and select Edit Project File
You should see this metapackage if ASP.NET core webapps the using statement
<PackageReference Include="Microsoft.AspNetCore.App" />
Microsoft.EntityFrameworkCore.SqlServer is included in this metapackage. So in your Startup.cs you may only need to add:
using Microsoft.EntityFrameworkCore;
In Visual Studio, check the NuGet Package Manager => Manage Packages for Solution, check all this packages, whether got installed in your solution or not, as below:
EntityFrameworkCore
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.InMemory
Microsoft.EntityFrameworkCore.Relational
Microsoft.EntityFrameworkCore.Sqlite.Core
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
I solved the same issues after check all the above packages have been installed.
Install package, EntityFrameworkCore.SqlServer:
PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.3
Nuget:
https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer/
I also had the same problem. I added the following. It works for me
Microsoft.EntityFrameworkCore.SqlServer
In my case :-
I have hit the below command and it is resolved.
Command
Install-Package Microsoft.EntityFrameworkCore.SqlServer -v 1.1.1
I add SqlServer and Sqlite to my project, the same problem arises.
For me, I installed Microsoft.EntityFrameworkCore earlier, it's version is 5.0.6. Now Microsoft.EntityFrameworkCore.SqlServer version is 5.0.7. There is no UserSqlServer method after installation.
1. Try to upgrade the tool version to be consistent
Try to modify the version in csproj:
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.7">
Or open NuGet to update the package.
2. Restart your Visual Studio
If it won't help you, the most important thing is to restart VS
And then, everything is OK.
reference
https://github.com/dotnet/efcore/issues/7891
For me this issue happened with Visual Studio Code and I was able to fix with 2 steps:
Manually adding using Microsoft.EntityFrameworkCore;
Running dotnet build in terminal.
first add Install-Package Microsoft.EntityFrameworkCore.SqlServer
next add in your .cs file using Microsoft.EntityFrameworkCore;
finally add this in your core Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFrameworkSqlServer().AddDbContext<ApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MovieContext")));
}
For anyone still having this problem:
Use NuGet to install:
Microsoft.EntityFrameworkCore.Proxies
This problem is related to the use of Castle Proxy with EFCore.
Wow so many answers yet none mentioned this Microsoft.EntityFrameworkCore.InMemory package!
Add the reference to this package:
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.2.2" /> and you should be good to go.
If you are facing this issue in case of Sqlite then
.
I think this is the problem with version of Sqlite,I had the same problem when I was using this versions of SqLite
Version 2.2.4:
After checking version here I changed the version then it worked.
No error after using this
Version 2.1.2:
Easy way to fix this issue
Error message:
Solution:
to install "microsoft.entityframeworkcore.sqlserver" with NuGet
Fixed :
PS: make sure you have using EF on the content "using Microsoft.EntityFrameworkCore;"
I read and did everything instructed in every answer and I just found out my problem was a missing constructor on my DbContext
public Context(DbContextOptions<Context> options) : base(options) { }
I hope this helps anyone facing the same problem I was.
I got around this by simply:
Add SqlServerDbContextOptionsExtensions to the class in question
Resolve SqlServerDbContextOptionsExtensions
This fixes the issue, must be missing some reference by default.
I had this trouble when I moved to Microsoft.EntityFrameworkCore.SqlServer v3.0.0 and Microsoft.EntityFrameworkCore.Tools v3.0.0
When I changed back to v2.2.6 on both libraries, the error went away. This is more of a workaround than a solution but it'll get you up and running till the issue is fixed.
Currently working with Entity Framework Core 3.1.3. None of the above solutions fixed my issue.
However, installing the package Microsoft.EntityFrameworkCore.Proxies on my project fixed the issue. Now I can access the UseLazyLoadingProxies() method call when setting my DBContext options.
Hope this helps someone. See the following article:
Lazy Loading in EF Core
For asp.net core version 2.1 make sure to add the following package to fix the problem. (At least this fix the issue using SQLite)
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
dotnet add package Microsoft.EntityFrameworkCore.Design
Here is the reference of the documentation using SQLite with entity framework core.
https://learn.microsoft.com/en-us/ef/core/get-started/netcore/new-db-sqlite
I had this issue, it seems that I hadn't added the required NuGet packages, although I thought I had done so, make sure to check them, one by one.
Install-Package:
**Microsoft.EntityFrameworkCore.SqlServer**
then add the top of your class:
**Microsoft.EntityFrameworkCore;**
that's worked for me

Migration: No DbContext was found in assembly

Using VS Community 2017. I have tried to create initial migration with error message saying:
Both Entity Framework Core and Entity Framework 6 are installed. The Entity Framework Core tools are running. Use 'EntityFramework\Add-Migration' for Entity Framework 6.
No DbContext was found in assembly 'Test_Project'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
... code in my dbcontext:
protected override void OnModelCreating(DbModelBuilder mb)
{
base.OnModelCreating(mb);
mb.Entity<Stuff>().ToTable("Stuff");
}
public DbSet<Stuff> Stuff{ get; set; }
In the Package Manager Console select the project where the DbContext is defined and run the command add-migration initial.
For example:public class SomeContext : DbContext
You have to specify the project name where the DbContext is located. So just right on the Nugget PM Console, type: Add-Migration MigrationName -Project YourProjectName.
Using the nuget package manager, I had installed all the EntityFrameworkCore dependencies, along with the EntityFramework 6 dependency. Visual Studio was using EF Core, when I needed it to be using EF 6. Uninstalling all EF Core dependencies resolved this issue.
When I had this issue, I had just renamed an existing project to "EF", then renamed all namespaces to "EF". What worked for me was I changed the Assembly name to something other than "EF" (Project properties/Application/Assembly Name). I was using VS Community 2019.
Just close the visual studio and restart your project and then write command again. It worked for me twice
I solved the problem by renaming the project, before that it coincided with the name of the solution.

With ServiceStack 3.9.56 cannot find the namespace ServiceStack.ServiceClient.Web

Using NuGet package manager I added the ServiceStack Clients package to a C# project. I tried to add the namespace reference "using ServiceStack.ServiceClient.Web" to a class but the namespace isn't available. I've removed the ServiceStack packages with NuGet, compiled the project and added them again, but the namespace ServiceStack.ServiceClient.Web isn't found.
I have a separate project which uses ServiceStack 3.9.49 and the exact same NuGet packages [ID: ServiceStack.Text & ServiceStack.Common] and the namespace is available in that version.
As this was a new project, I neglected to change the target framework from the client profile to the full/complete profile. Once I made the change, and compiled the app, the namespace was then available. Thanks.

EF 5 Enable-Migrations : No context type was found in the assembly

I have 4 projects :
Toombu.Entities : all models are there
Toombu.DataAccess: Mapping, Repository and ToombuContext
Toombu.Logique : Logic of my application
Toombu.Web : MVC 4 application. With all others DLL.
I tried to enable migration in Toombu.Web but i had this error :
No context type was found in the assembly
How can I enable migration ?
I am surprised that no one mentioned the obvious answer to this question: Entity Framework requires a context before enable-migrations will work. The error message the OP posted suggests that no context was found. Sure, it could be because the package manager console doesn't "see" the context--in which case the accepted answer is a possible solution (another solution is one I suggest, below). But a context must exist in the current project (assembly) before any other solutions will work.
What does it mean to have a context? It means that there must exist a class in your project that inherits from DbContext (in System.Data.Entity). Here is an example:
public class MyDbContext : DbContext
{
public MyDbContext()
{
}
}
Be sure you use
using System.Data.Entity;
before the code above has access to the DbContext class and that you have used NuGet to get Entity Framework 4.1 or later for the current project.
If all along you had a context but the Package Manager Console just doesn't "see" it: In Visual Studio 2013 you don't have to use the -ProjectName switch. Instead, go to the Package Manager Console (it's available in the View | Other Windows list), and look at the two dropdowns that appear at the top of the Package Manager Console dockable window. The first dropdown is for Package Source; the second is for Default Project. If you dropdown the Default Project and select a project in your solution then whatever commands you issue in the Package Manager console will be executed against the selected project.
use -ProjectName option in Package Manager Console:
Enable-Migrations -ProjectName Toombu.DataAccess -StartUpProjectName Toombu.Web -Verbose
Change the default project and choose the startup project from dropdown:
In my case, the NuGet package "Microsoft.EntityFrameworkCore.Tools" was missing
If anyone is still facing this problem. I solved it by using the following command:
Enable-Migrations -ProjectName <YOUR_PROJECT_NAME> -ContextTypeName <YOUR_CONTEXT_NAME>
Don't forget to use the full path to your context name.
You dbcontext is in Toombu.DataAccess So you should enable migrations in Toombu.DataAccess.
I created a Class in the Models directory called: myData with the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace Vidly.Models
{
public class MyDbContext : DbContext
{
public MyDbContext()
{
}
}
}
rebuilt the app with: control-shift-b
then ran the following in the nuGet Console:
Enable-Migrations -StartUpProjectName Vidly -ContextTypeName Vidly.Models.MyDbContext -Verbose
the Console returned:
Using StartUp project 'Vidly'.
Using NuGet project 'Vidly'.
Checking if the context targets an existing database...
Code First Migrations enabled for project Vidly.
Enable-Migrations -StartUpProjectName Vidly -ContextTypeName Vidly.Models.myData -Verbose
And the FrameWork created a Migrations directory and wrote a Configuration.cs template in there with the following code:
namespace Vidly.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<Vidly.Models.MyDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(Vidly.Models.MyDbContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data.
}
}
}
Follow the below steps to resolve the issue
Install-Package EntityFramework-IncludePrerelease
or Install entity framework from Nuget Package Manager
Restart visual studio
After that I was getting "No context type was found in assembly"
To resolve it - This "No context" that mean you need to create class in "Model" folder in your app with suffix like DbContext ... like this AppDbContext. There you need to include some library using System.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace Oceans.Models
{
public class MyDbContext:DbContext
{
public MyDbContext()
{
}
}
}
After that run the below command on Package Manager:
Enable-Migrations -ProjectName <YourProjectName> -ContextTypeName <YourContextName>
My Project Name is - MyFirstApp and AppDbContext is inside the Model Folder so path is like
Enable-Migrations -StartUpProjectName MyFirstApp -ContextTypeName MyFirstApp.Models.AppDbContext
In mosh tutorial, individual user account was selected which created a db context in the template.
Also, make sure EntityFramework is installed in the Nuget package manager.
If you use Both Entity Framework 6 and Entity Framework Core are installed. The Entity Framework 6 tools are running.
Use EntityFrameworkCore\Enable-Migrations for Entity Framework Core. same as for add migration and update database.
Thanks for the suggestions, I solved the problem by combining all the solutions here. At first I created the DbContext Model:
public class MyDbContext: DbContext
{
public MyDbContext()
{
}
}
After creating the dbcontext class, I ran the enable-migration command with the project Name: enable-migrations -ProjectName YourProjectName
I had to do a combination of two of the above comments.
Both Setting the Default Project within the Package Manager Console, and also Abhinandan comments of adding the -ContextTypeName variable to my full command. So my command was as follows..
Enable-Migrations -StartUpProjectName RapidDeploy -ContextTypeName RapidDeploy.Models.BloggingContext -Verbose
My Settings::
ProjectName - RapidDeploy
BloggingContext (Class Containing DbContext, file is within Models folder of Main Project)
My problem was link---->
problem1
I solved that problem with one simple command line
Install-Package EntityFramework-IncludePrerelease
After that, i needed to face with one more problem, something like:
"No context type was found in assembly"
I solve this really easy. This "No context" that mean you need to create class in "Model" folder in your app with suffix like DbContext ... like this MyDbContext.
There you need to include some library using System.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace Oceans.Models
{
public class MyDbContext:DbContext
{
public MyDbContext()
{
}
}
}
After that,i just needed this command line:
Enable-Migrations -ProjectName <YourProjectName> -ContextTypeName <YourContextName>
I got this problem first:
PM> add-migration first
No migrations configuration type was found in the assembly 'MyProjectName'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).
then i tried this:
PM> Enable-Migrations
No context type was found in the assembly 'MyProjectName'.
Then the right command for me :
PM> Enable-Migrations -ProjectName MyProjectName -ContextTypeName MyProjectName.Data.Context
After that i got this error message even though Context inherits from DbContext
The type 'Context' does not inherit from DbContext. The DbMigrationsConfiguration.ContextType property must be set to a type that inherits from DbContext.
Then i Installed
Microsoft.EntityFrameworkCore.Tools
ITS OK NOW but the message is funny. i already tried add migrations at first :D
Both Entity Framework Core and Entity Framework 6 are installed. The Entity Framework Core tools are running. Use 'EntityFramework6\Enable-Migrations' for Entity Framework 6.
Enable-Migrations is obsolete. Use Add-Migration to start using Migrations.
Change the default project to data access
change the default project dropdown in the package manager console to data access and give enable migrations...
Thats all success
Using the Package Manager, you need to re-install Entity Framework:
Uninstall-Package EntityFramework -Force
Then install it for each project:
Install-Package EntityFramework
Then do not forget to restart the studio.
Ensure you are using the same version of Entity Framework across all projects using the NuGet Package Manager.
Recent windows updates may have installed a newer version of Entity Framework into your active project.
Background:
Around 16 Mar 2016, I started getting this error when trying to add migrations to a project where I had already enabled migrations and had successfully done migrations for.
I noticed that around March 10, a new stable version of Entity Framework 6 had been released.
If I specified the -ContextTypeName parameter in the enable-migrations command, I got an error indicating the migrations were already enabled.
Resolution:
1) Tools -> Nuget Package Manager -> Manage Nuget Packages for Solution
2) (Not sure if this step is necessary, but..) I updated my version of the Nuget Package Manager to the latest version. Also, after updating my version of Nuget Package Manager, I had to restart Visual Studio twice before the NuGet Command line would work properly.
3) Tools -> Nuget package Manager -> Manage Nuget Packages for Solution -> Search Installed packages -> Type Entity Framework
a. You may see more than one version of Entity Framework there.
b. Click Manage on each version of Entity Framework and ensure that your projects are using the SAME version of Entity Framework.
Uncheck the version of Entity Framework that you are not using and for the version of Entity Framework you ARE using make sure it is checked across your projects that need it.
Again, as noted in step 2, I had to restart visual studio twice to get the NuGet Package Manager Console to work properly after updating my version of the NuGet Package Manager. I got an error starting the console the first time, and
"exception calling createinstancefrom with 8 arguments could not load file or assembly EntityFramework" when running the enable-migrations command the second time.
Restarting visual studio seemed to resolve those issues, however.
This error getting because of the compiler not getting 'Context' class in your application. So, you can add it manually by Add --> Class and inherit it with 'DbContext' Class
For Example :
public class MyDbContext : DbContext
{
public DbSet<Customer> Customer { get; set; }
public MyDbContext()
{
}
}
I have been getting this same problem. I have even tried above enable migrations even though I have already done. But it keeps giving same error. Then I had to use the force switch to get overcome this problem. I am sure this will help in someone else's case as well as its a possible work around.
After enabling migration with force, you should update your database (Make sure default project is set correctly). Otherwise you will get another problem like explicit migrations are pending.
Then just execute your add-migrations or any other commands, it should work.
Enable-Migrations -ProjectName <PROJECT_NAME> -ContextTypeName <FULL_CONTEXT_NAMESPACE.YOUR_CONTEXT_NAME> -force
Adding a class which inherits DbContext resolved my problem:
public class MyDbContext : DbContext { public MyDbContext() { } }
How to Update table and column in mvc using entity framework code first approach
1: tool > package manager console
2: select current project where context class exist
3: Enable migration using following command
PM > enable-migrations
4: Add migration folder name using following command
PM > add-migration MyMigrationName
4: Now update database following command
PM > update-database
enable-migrations -EnableAutomaticMigration:$false with this command you can enable migration at Ef 6.3 version because C# enable as default migrations at Ef 6.3 version.
I have encountered this problem a few times and in my case I uninstalled EntityFramework nuget package and installed EntityFrameworkCore nuget package, entityFramework.design and entityframework.tools
I got the same error when I had Authentication disabled/chose "No Authentication'. I re-made my project and chose "Individual User Accounts" and I didn't get the error anymore.
When I faced the same problem, I found that I had renamed my project in the solution explorer.
I needed to open the project in notepad and change the old name to new name.
OPs question was for EF5; I had the same problem with EF6, and my experience was quite similar. Multiple answers here reference EntityFrameworkCore, but using that was a huge misdirect for me.
It seems many things can cause the OPs error; I think both jazimov and Sadjad Khazaie presented good solutions that are useful for both EF and EFCore. However, when I had EFCore installed alongside EF6, that actually CAUSED this problem. It seems that my existing EF6 codebase was using EF6 migrations, and with EntityFrameworkCore packages installed, I got the No context type was found in the assembly error because the EFCore add-migration command was running.
When I removed the EntityFrameworkCore packages, the problem went away.
Note: sometimes I got a warning that both EntityFrameworkCore and EntityFramework were installed when I ran add-migration, but not always. One way to be sure: try enable-migrations, which is available with EntityFramework but is not available (or necessary) with EntityFrameworkCore.
Create a File called MyDBContext inside Models Folder
using System.Data.Entity;
namespace VSR.Models
{
public class MyDbContext: DbContext
{
public MyDbContext()
{
}
}
}
Now Try to execute Enable-migrations. It will work.
namespace EntityFrameworkCodeFirst.Module
{
public class MyDbContext: DbContext
{
public MyDbContext()
{
}
}
}
And if you have Multiple project in one solution than you have to use below commands:-
Enable-Migrations -ProjectName EntityFrameworkCodeFirst
Worked for me:
UnInstall-Package EntityFramework
Restart Visual Studio
Install-Package EntityFramework
Build project

EF 6 System.Data.Objects.ObjectContext Error

I recently upgraded from Entities Framework 5 to Entities Framework 6 Alpha 2 and I am getting the following error:
Method not found: 'System.Data.Objects.ObjectContext
System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()'.
This is getting hit when I call
if (Membership.ValidateUser(model.UserName, model.Password)) {}
This used to work fine before not sure why it's springing this error. Any suggestions?
EF 6 does not have System.Data.Objects.ObjectContext. EF 6 has moved some types, including ObjectContext, from System.Data.Entity.dll into EntityFramework.dll, and changed their namespaces. The fact that you get this error suggests you haven't attempted to recompile your application, you've simply replaced EntityFramework.dll and hoped for the best. That won't work. You need to update your code to work with EF 6: you need to remove your references to System.Data.Entity.dll, and update your code to refer to the new types.
It just might be possible for the reference to the IObjectContextAdapter.ObjectContext property to be in some library you're using, but most likely it'll be in your own code. The error message (in the part you didn't include in your question) should tell you where it is coming from.
For me updating these below worked:
using System.Data.Objects; --> using System.Data.Entity.Core.Objects;
using System.Data.Objects.DataClasses; --> using System.Data.Entity.Core.Objects.DataClasses;
I'm also using EF 6.
I managed to solve the problem uninstalling the package Microsoft.AspNet.Providers.Core v. 1.2. I'm using version 1.1 instead. If you're like me and is using LocaDb, you'll have to uninstall the LocaDb package because it depends on that package. Of course you'll have to reinstall LocaDb again...
You can grab v. 1.1 using the NuGet Package Manager Console inside Visual Studio:
Install-Package Microsoft.AspNet.Providers.Core -Version 1.1
There's a Microsoft Connect bug filled regarding this issue:
Microsoft.AspNet.Providers.Core incompatible with EF6
The new 2.0 version of the providers (http://www.nuget.org/packages/Microsoft.AspNet.Providers.Core/) are EF6 compatible (they'll actually only work with EF6).
I managed to resolve this by removing the AspNet Providers I had installed through Nuget, which was marked as deprecated. Doing this also uninstalled Entity Framework.
I then installed the new AspNet Universal Providers, followed by Entity Framework 6, and all my problems were fixed.
Check This Link
http://visualstudiomagazine.com/articles/2014/03/01/whats-new-in-entity-framework-6.aspx
I Updated the EF 6.2 and get same error and find the solution as fallows
Change the namespace System.Data.Entity to System.Data.Entity.Core, including any references to System.Data.* namespaces (for example, System.Data.Objects becomes System.Data.Entity.Core.Objects).
That happens when entity framework is unable to find the method in the dotnet framework library installed in the machine. So install dotnet framework 4.5.2 or higher. It will fix the issue.
What worked for me was the following:
Install the dll 'Microsoft.AspNet.DataSource' with:
PM> Install-Package Microsoft.AspNet.EntityDataSource
Reference 'Microsoft.AspNet.DataSource.dll' in your project.
Added the following using declarations:
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Core.Objects;
using Microsoft.AspNet.EntityDataSource;
Removed the following using declarations:
using System.Data.Entity;
using System.Data.Metadata.Edm;
using System.Data.Objects;
using System.Web.UI.WebControls;
Voila, code is compiling and working.
It has an old version associated with edmx file for this:
Reinstall EF with Nuget
Delete the .edmx file and recreate it with tables
A quick and simple fix for me was to remove the offending assemblies (deprecated) and added a reference to the new library. The code was modified within the Context.tt and looks like this:
if (container.FunctionImports.Any())
{
#>
using System.Data.Entity.Core.Objects; // The assembly you need
using System.Linq;
<#
}
Before any modifications it had appeared as such:
if (container.FunctionImports.Any())
{
#>
using System.Data.Objects; // Error on compile
using System.Data.Objects.DataClasses; // Error on compile
using System.Linq;
<#

Categories

Resources