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
22,757
questions
548
votes
18
answers
405k
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 ...
464
votes
21
answers
602k
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 '...
384
votes
36
answers
404k
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 ...
325
votes
11
answers
322k
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, ...
287
votes
26
answers
379k
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 ...
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 ...
264
votes
22
answers
336k
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 ...
227
votes
22
answers
296k
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 {...
212
votes
10
answers
204k
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....
186
votes
7
answers
200k
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 ...
175
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 ...
174
votes
7
answers
212k
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 ...
169
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;...
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");
...
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?