Skip to main content

Questions tagged [entity-framework-core]

For questions about Entity Framework Core (EF Core) for .NET Core. Please also add a version-specific [ef-core-xx] tag, when applicable. Note that Entity Framework for .NET Framework is an older, different product that fits the [entity-framework] tag.

entity-framework-core
Filter by
Sorted by
Tagged with
546 votes
18 answers
404k views

Command dotnet ef not found

I'm following the docs in order to create an initial migration. When I execute dotnet, I get the help section, meaning that the PATH works properly. Then I try to execute the command below from the ...
Konrad Viltersten's user avatar
462 votes
20 answers
600k views

How to unapply a migration in ASP.NET Core with EF Core

When I run PM> Remove-Migration -context BloggingContext in VS2015 with an ASP.NET Core project using EF Core, I get the following error: System.InvalidOperationException: The migration '...
nam's user avatar
  • 23.3k
384 votes
36 answers
403k views

Entity Framework Core: DbContextOptionsBuilder does not contain a definition for 'usesqlserver' and no extension method 'usesqlserver'

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 ...
Maxime Laflamme's user avatar
324 votes
11 answers
321k views

Entity Framework Core add unique constraint code-first

I can't find way to add a unique constraint to my field with using attribute: public class User { [Required] public int Id { get; set; } [Required] // [Index("IX_FirstAndSecond", 2, ...
Vadim M's user avatar
  • 3,295
287 votes
26 answers
378k views

The term "Add-Migration" is not recognized

I'm using this MSDN Tutorial to run in VS2015 the command PM> Add-Migration MyFirstMigration -context BloggingContext that ran yesterday successfully but today it's giving the following error that ...
nam's user avatar
  • 23.3k
278 votes
15 answers
176k views

Need to Update EF Core Tools

When I use the dotnet ef tools in the VS 2017 Package Manager Console I get a warning message about needing to update EF Core tools: PM> dotnet ef migrations list -s ../RideMonitorSite The EF ...
user avatar
264 votes
22 answers
335k views

Raw SQL Query without DbSet - Entity Framework Core

With Entity Framework Core removing dbData.Database.SqlQuery<SomeModel> I can't find a solution to build a raw SQL Query for my full-text search query that will return the tables data and also ...
David Harlow's user avatar
  • 2,823
226 votes
22 answers
295k views

.NET Core 3.0 possible object cycle was detected which is not supported

I have two entities that are related as one-to-many: public class Restaurant { public int RestaurantId {get;set;} public string Name {get;set;} public List<Reservation> Reservations {...
Nazar  Pylyp's user avatar
  • 2,363
212 votes
10 answers
203k views

Get SQL code from an Entity Framework Core IQueryable<T>

I am using Entity Framework Core and I need to see which SQL code is being generated. In previous versions of Entity Framework I could use the following: string sql = ((System.Data.Objects....
Miguel Moura's user avatar
  • 38.5k
186 votes
7 answers
199k views

How to auto create database on first run?

My application being ported to .NET Core will use EF Core with SQLite. I want to automatically create the database and tables when the app is first run. According to documentation this is done using ...
deandob's user avatar
  • 5,230
174 votes
7 answers
211k views

Can I generate script of a migration with EF code first and .net core

I'm building a MVC application with .Net Core and I need to generate the script of a migration. With EF6 I did run the command update-database -script but when I try to do the same with .net Core is ...
Gabriel Castillo Prada's user avatar
174 votes
9 answers
131k views

Filtering on Include in EF Core

I'm trying to filter on the initial query. I have nested include leafs off a model. I'm trying to filter based on a property on one of the includes. For example: using (var context = new ...
Jason N. Gaylord's user avatar
167 votes
2 answers
69k views

Include several references on the second level

Assume we have this model : public class Tiers { public List<Contact> Contacts { get; set; } } and public class Contact { public int Id { get; set; } public Tiers Tiers { get; set;...
Christophe Gigax's user avatar
164 votes
16 answers
145k views

EF Core Mapping EntityTypeConfiguration

In EF6 we usually able to use this way to configure the Entity. public class AccountMap : EntityTypeConfiguration<Account> { public AccountMap() { ToTable("Account"); ...
Herman's user avatar
  • 2,898
163 votes
9 answers
139k views

How and where to call Database.EnsureCreated and Database.Migrate?

I have a ASP.NET Core 6 MVC application, and I need to call the Database.EnsureCreated and Database.Migrate methods. But where should I call them?
bailando bailando's user avatar
162 votes
12 answers
303k views

Unable to create an object of type '[DBContext's Name]'. For the different patterns supported at design time [closed]

I'm following one of Mosh Hamedani Course on ASP.NET MVC in Udemy. I came across one error while designing my Database using code-first (Entity Framework). At first, I got the error of " No ...
OoMaR jOhUr's user avatar
  • 1,651
161 votes
10 answers
357k views

How to update record using Entity Framework Core?

What is the best approach to update database table data in Entity Framework Core? Retrieve the table row, do the changes and save Use keyword Update in DB context and handle exception for item not ...
CuriousGuy's user avatar
  • 4,038
157 votes
14 answers
108k views

EntityTypeBuilder does not contain a definition for ToTable in EF Core

I have this sample code: using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Models; namespace MySampleNamespace { public class MyDbContext : ...
g_b's user avatar
  • 12.2k
151 votes
19 answers
245k views

Entity Framework Core: `SqlNullValueException: Data is Null.` How to troubleshoot?

I am using Entity Framework Core in an ASP.NET Core application and Controller action and I haven't changed something to the working code nor to the database but I can't tell what is the query ...
Natalie Perret's user avatar
147 votes
3 answers
80k views

How to call ThenInclude twice in EF Core?

I'm creating an ASP.NET Core API app, and relying on EF Core. I have entities defined like this: public class AppUser : IdentityUser { public string FirstName { get; set; } public string ...
tinker's user avatar
  • 3,184
144 votes
13 answers
148k views

How do you show underlying SQL query in EF Core?

At 3:15 from the end of this ".NET Core 2.0 Released!" video, Diego Vega shows a demo of new features in Entity Framework Core 2.0. As part of that, a dump of the underlying SQL is shown in ...
Gigi's user avatar
  • 29.2k
136 votes
13 answers
163k views

How to store JSON in an entity field with EF Core?

I am creating a reusable library using .NET Core (targeting .NETStandard 1.4) and I am using Entity Framework Core (and new to both). I have an entity class that looks like: public class Campaign { ...
Alex's user avatar
  • 2,895
129 votes
24 answers
184k views

"Build failed" on Database First Scaffold-DbContext

I'm trying to generate classes from a database (EntityFramework's database first approach). For convenience, I'm more or less walking along with this tutorial: https://docs.efproject.net/en/latest/...
LightToTheEnd's user avatar
125 votes
31 answers
179k views

Your startup project doesn't reference Microsoft.EntityFrameworkCore.Design

I have 2 projects in my solution, I have a project with Entity Framework Core installed: And in the other ASP.NET Web API project I have these packages: <?xml version="1.0" encoding="utf-8"?> &...
el_pup_le's user avatar
  • 12k
124 votes
7 answers
33k views

How can I reset an EF7 InMemory provider between unit tests?

I am trying to use the EF7 InMemory provider for unit tests, but the persistent nature of the InMemory database between tests is causing me problems. The following code demonstrates my issue. One test ...
Sailing Judo's user avatar
  • 11.2k
123 votes
23 answers
168k views

Add migration with different assembly

I am working on a project with ASP.NET CORE 1.0.0 and I am using EntityFrameworkCore. I have separate assemblies and my project structure looks like this: ProjectSolution -src -1 Domain ...
kdar's user avatar
  • 1,261
121 votes
2 answers
136k views

Entity framework EF.Functions.Like vs string.Contains

I was reading the announcement of entity framework core 2.0 https://blogs.msdn.microsoft.com/dotnet/2017/08/14/announcing-entity-framework-core-2-0/ It says that they added new Sql functions like EF....
areller's user avatar
  • 5,048
117 votes
5 answers
128k views

How to set command timeout in ASP.NET Core / Entity Framework Core?

The place where the command timeout is set is no longer the same as earlier versions. However, I cannot find anywhere that says how to change this. What I am doing is uploading very large files which ...
Greg Gum's user avatar
  • 36.6k
115 votes
30 answers
132k views

Unable to create migrations after upgrading to ASP.NET Core 2.0

After upgrading to ASP.NET Core 2.0, I can't seem to create migrations anymore. I'm getting "An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the ...
ruhm's user avatar
  • 1,367
114 votes
16 answers
300k views

Error: the entity type requires a primary key

I would like to expand the question asked on this thread Binding listbox to observablecollection by giving it an ability to persistent the data. The structure is mostly the same except I installed ...
user1205746's user avatar
  • 3,296
113 votes
5 answers
88k views

in entity framework code first, how to use KeyAttribute on multiple columns

I'm creating a POCO model to use with entity framework code first CTP5. I'm using the <key()> decoration to make a property map to a PK column. But how can I define a PK on more then one ...
GilShalit's user avatar
  • 6,361
113 votes
21 answers
263k views

How to run stored procedures in Entity Framework Core?

I am using EF Core 1.0 in an ASP.NET Core App. Can you please point me to the proper way of executing stored procedures? The old method with ObjectParameters and ((IObjectContextAdapter)this)....
eadam's user avatar
  • 25.2k
109 votes
5 answers
170k views

Entity framework Core Update-database specific migration

I am trying to figure out how to run a specific migration from the package manager in nuget. I have tried to run: update-database -TargetMigration test32 But I do get this message: A parameter ...
stian64's user avatar
  • 1,633
109 votes
11 answers
77k views

How to mock an async repository with Entity Framework Core

I'm trying to create a unit test for a class that calls into an async repository. I'm using ASP.NET Core and Entity Framework Core. My generic repository looks like this. public class ...
Jed Veatch's user avatar
  • 2,376
106 votes
5 answers
88k views

navigation property should be virtual - not required in ef core?

As I remember in EF navigation property should be virtual: public class Blog { public int BlogId { get; set; } public string Name { get; set; } public string Url { get; set; } ...
Alexan's user avatar
  • 8,485
101 votes
6 answers
67k views

ef core doesn't use ASPNETCORE_ENVIRONMENT during update-database

I use visual studio to update all my environments with a certain migration. It had worked fine using the command below. update-database -Migration initMigrationProduct -c ProductContext -Environment ...
joakimja's user avatar
  • 2,866
100 votes
10 answers
158k views

How to update the model when using database first approach

I used EntityFramework Core database first to create model as illustrated in the EF Core documentation But I don't know how to update the model when the database has been edit.
witson's user avatar
  • 1,103
98 votes
7 answers
116k views

Mocking EF core dbcontext and dbset

I am using ASP.NET Core 2.2, EF Core and MOQ. When I run the test I am getting this error: Message: System.NotSupportedException : Invalid setup on a non-virtual (overridable in VB) member: x => x....
MarcosF8's user avatar
  • 1,978
93 votes
13 answers
77k views

ASP - Core Migrate EF Core SQL DB on Startup

Is it possible to have my ASP Core Web API ensure the DB is migrated to the latest migration using EF Core? I know this can be done through the command line, but I want to do it programatically.
Zeus82's user avatar
  • 6,225
93 votes
6 answers
235k views

Auto-increment on partial primary key with Entity Framework Core

I have declared the following model using the EF Core fluent API: modelBuilder.Entity<Foo>() .HasKey(p => new { p.Name, p.Id }); Both fields are marked as the primary key when I create ...
Martin's user avatar
  • 2,695
92 votes
10 answers
218k views

Cannot access a disposed object. A common cause of this error is disposing a context

I have written a simple application and when I navigate to my edit page the below error pops up. Microsoft.EntityFrameworkCore.Query[10100] An exception occurred while iterating over the results of ...
Soheil's user avatar
  • 1,390
92 votes
1 answer
59k views

AddDbContext or AddDbContextPool

For Asp.net Core apps, which one do we have to use? AddDbContext or AddDbContextPool? According to EF Core documentation, AddDbContextPool provides high performance but the default Asp.net Core ...
Display Name's user avatar
  • 14.9k
89 votes
5 answers
128k views

How to instantiate a DbContext in EF Core

I have setup .net core project and db context also. But i cant start using dbContext yet due this error- "there is no argument given that corresponds to the required formal parameter 'options'&...
john Cogdle's user avatar
  • 1,503
89 votes
19 answers
187k views

Could not load file or assembly Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.0.0

After update to the new package Microsoft.EntityFrameworkCore.SqlServer 1.1.2 I got error when try to create DBContext: System.IO.FileLoadException occurred HResult=0x80131040 Message=Could ...
Robert N. Dean's user avatar
88 votes
3 answers
127k views

What goes into DbContextOptions when invoking a new DbContext?

I am not using DI and simply want to invoke a DbContext from within my controller. I am struggling to figure out what the 'options' should be? ApplicationDbContext.cs public class ...
Reza's user avatar
  • 5,558
83 votes
1 answer
45k views

ASP.NET add migration 'composite primary key error' how to use fluent API

Hello I am in the process of creating a Web Application and have already installed both the Microsoft.entityFrameworkCore and Microsoft.entityFrameworkCore.Tools. During the process of executing an ...
RyeGuy's user avatar
  • 4,383
83 votes
3 answers
47k views

AddAsync() vs Add() in EF Core

Ok multiple questions here: I am trying to understand what is the difference (outside the obvious asynchronous) between AddAsync() and Add() methods in EF Core? When do you choose one over the other?...
Aeseir's user avatar
  • 8,180
82 votes
5 answers
56k views

How to get Table Name of mapped entity in Entity Framework Core

In some reason, I need to use SQL in EFCore, and I will use table name of mapped entity. How can I get it?
Andrew Cui's user avatar
  • 1,419
79 votes
7 answers
166k views

Working with SQL views in Entity Framework Core

For example, I have such model: public class Blog { public int BlogId { get; set; } public string Url { get; set; } public BlogImage BlogImage { get; set; } } public class BlogImage { ...
Yurii N.'s user avatar
  • 5,613
79 votes
2 answers
83k views

Entity Framework Core: How to get the Connection from the DbContext?

I am trying the new Entity Framework Core with MySQL Connector. I can get a valid DbContext and write into the database so everything has been setup correctly. I need to get the Connection from ...
nico9T's user avatar
  • 2,656

1
2 3 4 5
455