All Questions
Tagged with .net-core entity-framework-core
3,234
questions
522
votes
16
answers
379k
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 ...
458
votes
20
answers
571k
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 '...
183
votes
7
answers
193k
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 ...
158
votes
10
answers
343k
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 ...
144
votes
17
answers
229k
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 ...
135
votes
3
answers
75k
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 ...
119
votes
2
answers
130k
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....
97
votes
6
answers
63k
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 ...
91
votes
10
answers
200k
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 ...
88
votes
18
answers
178k
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 ...
87
votes
4
answers
121k
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'&...
79
votes
5
answers
54k
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?
76
votes
2
answers
80k
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 ...
75
votes
3
answers
63k
views
Equivalent for .HasOptional in Entity Framework Core 1 (EF7)
Consider two classes.
public class File
{
[Key]
public string Id { get; set; }
public string Message_Id { get; set; }
internal Message Message { get; set; }
}
public class Message
...
73
votes
1
answer
48k
views
AddDbContext was called with configuration, but the context type 'MyContext' only declares a parameterless constructor?
In the following console application (.Net core 2.0), the scaffold-dbcontext created the following DbContext
public partial class MyContext : DbContext
{
public virtual DbSet<Tables> Tables ...
71
votes
1
answer
75k
views
Cascade deleting with EF Core
I am having a few issues with EF Core at the moment. I have some data that I need to delete, and I am struggeling to see how the fluent API works, exactly in regards to the .OnDelete() function.
...
68
votes
14
answers
119k
views
EF Core Error - No project was found. Change the current working directory or use the --project option
I am using Visual Studio 2015 and dotnet core and trying to develop an EF Core Code First project using Sqlite and this documentation / tutorial, which also uses Sqlite => NET Core - New Database
...
60
votes
1
answer
25k
views
Purpose of package "Microsoft.EntityFrameworkCore.Design"
All tutorials agree that project.json should include:
"Microsoft.EntityFrameworkCore.Design":
{
"type":"build",
"version":"1.0.0-preview2-final"
}
I have never included it, and have ...
59
votes
9
answers
88k
views
How to refresh an Entity Framework Core DBContext?
When my table is updated by another party, the db context in dotnet core still return the old value, how can I force the Db context to refresh?
I've done research but I only found people use Reload ...
57
votes
1
answer
30k
views
How to get connectionString from EF Core 2.0 DbContext
In EF6 works this code:
public string GetConnectionString(DbContext ctx)
{
ObjectContext _objectContext = ((IObjectContextAdapter)ctx).ObjectContext;
if (_objectContext?....
55
votes
9
answers
54k
views
How to seed in Entity Framework Core 2?
I have two tables, and I want to fill them using seeds.
I use ASP.NET Core 2 in Ubuntu.
How to populate the data for the two tables where one is connected to the other via a foreign key?
The Flowmeter ...
48
votes
3
answers
23k
views
How do I implement DbContext inheritance for multiple databases in EF7 / .NET Core
I am building web APIs in ASP.NET Core 1.1.
I have a number different databases (for different systems) which have common base schemas for configuration items such as Configuration, Users and groups (...
47
votes
2
answers
24k
views
How to isolate EF InMemory database per XUnit test
I am trying use InMemory EF7 database for my xunit repository test.
But my problem is that when i try to Dispose the created context the in memory db persist. It means that one test involve other.
...
43
votes
3
answers
14k
views
How to use C# 8.0 Nullable Reference Types with Entity Framework Core models?
I am enabling C# 8.0 Nullable Reference Types on a .NET Core 3.0 project. The project uses Entity Framework Core 3.0 to access database.
The following is a data model whose Title should not be null.
...
42
votes
3
answers
31k
views
EF Core no .Include() method on DBset
I'm currently completely unable to call .Include() and intellisense (in vscode) doesn't seem to think it exists.
Now after a long time searching the web I've found this:
Not finding .Include() ...
41
votes
7
answers
48k
views
Command line connection string for EF core database update
Using ASP.NET Core and EF Core, I am trying to apply migrations to the database. However, the login in the connection string in appsettings.json that the app will use has only CRUD access, because of ...
40
votes
5
answers
45k
views
The child/dependent side could not be determined for the one-to-one relationship
I am trying to update my database with "update-database" command in package manager console, But I have this kind of error:
The child/dependent side could not be determined for the one-to-one
...
39
votes
9
answers
68k
views
Type Load Exception in EF Core Project
I have an ASP.NET Core 3.1 Web API application using EF Core. This is the my configuration in the ConfigureServices method of the Startup class:
services.AddDbContext<ApplicationContext>(options ...
38
votes
13
answers
44k
views
The term 'Add-migration' is not recognized - VS2017 and EntityFrameworkCore
I am posting this question just in case the solution I found would help someone else out. While working in Visual Studio 2017 rc4 each time I tried to run the Add-Migration command in the Package ...
37
votes
1
answer
33k
views
EF Core One to One or Zero Relationship
I have Person and Address. Address is optional.
Please see below code
class Person
{
[Key]
public int PersonID { get; set; }
public string Name { get; set; }
public Address Address { ...
36
votes
5
answers
34k
views
EF Core - Table '*.__EFMigrationsHistory' doesn't exist
I want to stress out that this is .NET Core and the threads about EF 6.0 does not apply to this problem
I created my DbContext and added it in DI, however when I do dotnet ef database update -v it ...
35
votes
8
answers
32k
views
Cannot install dotnet-ef tool on Windows 10
I'm trying to install the dotnet-ef tool via the dotnet-cli.
The command that I enter: dotnet tool install --global dotnet-ef
I gives me the following error:
The tool package could not be restored.
...
34
votes
5
answers
6k
views
Owned type mapping EF Core fails when saving
I want to make TableSplitting using Owned Types. I have the following model:
public class Account
{
public GUID Id { get; set; }
public string Email { get; set; }
public StreetAddress Address {...
33
votes
7
answers
31k
views
How to persist a list of strings with Entity Framework Core?
Let us suppose that we have one class which looks like the following:
public class Entity
{
public IList<string> SomeListOfValues { get; set; }
// Other code
}
Now, suppose we want to ...
33
votes
6
answers
42k
views
EF Core 'another instance is already being tracked'
I have a problem updating an entity in .Net Core 2.2.0 using EF Core 2.2.3.
An error occurred while saving changes. Error details:
The instance of entity type 'Asset' cannot be tracked because ...
32
votes
2
answers
30k
views
How to get user information in DbContext using Net Core
I am trying to develop a class library in which i want to implement custom DbContext. In the SaveChanges method of the DbContext, i need to get current user’s information(department, username etc.) ...
32
votes
6
answers
34k
views
Entity Framework Core migration - connection string
I'm having a problem to handle the DB connection string in conjunction with migrations.
I have 2 projects:
Domain
Application
The DbContext is in the Domain project, so this is the project I run ...
31
votes
4
answers
30k
views
Where is the EDMX
Getting exposed to dotnet Core. In a sample test application trying to setup EntityFramework.Core in dotnet core app. While I was able to add the EntityFramework.Core NugGet package I can't find the ...
30
votes
1
answer
16k
views
Lost parameter value during SQL trace in EF Core
I have implemented an approach for tracing SQL queries from EF Core according to this article: https://learn.microsoft.com/en-us/ef/core/miscellaneous/logging. And have problems with tracing query ...
30
votes
1
answer
14k
views
Where is IDbSet<T> in entity core
public abstract class RepositoryBase<T> : IRepository<T> where T : class
{
private ShopCoreDbContext dbContext;
private readonly DbSet<T> dbSet; //here
protected ...
29
votes
2
answers
20k
views
How should I manage DbContext Lifetime in MVC Core?
From the Documentation
Entity Framework contexts should be added to the services container
using the Scoped lifetime. This is taken care of automatically if you
use the helper methods as shown ...
28
votes
8
answers
65k
views
How do you configure the DbContext when creating Migrations in Entity Framework Core?
Is there way that dependency injection can be configured/bootstrapped when using Entity Framework's migration commands?
Entity Framework Core supports dependency injection for DbContext subclasses. ...
27
votes
2
answers
60k
views
Entity Framework : join two tables and where clause
I'm having trouble working with Entity Framework and PostgreSQL, does anybody know how to join two tables and use the second table as a where clause?
The select I want to do in Entity Framework would ...
26
votes
2
answers
9k
views
Issues with SQL Alias in ConnectionString in appsettings.json
In my appsettings.json, when I use this snippet:
"ConnectionStrings": {
"CssDatabase": "Server=BLUEJAY\\MSSQLSERVER2014;Database=CSS;Trusted_Connection=True;"
}
I can connect to the db as ...
26
votes
2
answers
16k
views
In EF Core 5, how can I insert an entity with a many to many relation by setting only the foreigns keys IDs, without querying first?
The other table contain references data with well know ID. The use case is to read data from file, create entities then insert them in batch. I don't need to query anything first, so all entities are &...
25
votes
6
answers
40k
views
How to get Column name and corresponding Database Type from DbContext in Entity Framework Core
Suppose I have this table:
How can I get the column name and database datatype from DbContext in Entity Framework Core?
Tips
The column with name clg# converted to clg1 by EF Core Scaffold tool so ...
25
votes
4
answers
24k
views
How to write Repository method for .ThenInclude in EF Core 2
I'm trying to write a repository method for Entity Framework Core 2.0 that can handle returning child collections of properties using .ThenInclude, but I'm having trouble with the second expression. ...
25
votes
1
answer
24k
views
Entity Framework Core: many-to-many relationship with same entity
I am trying to map many-to-many relationship with the same entity. The User entity has an IList<User> data field for Contacts, which stores users' contacts/friends information:
public class ...
24
votes
4
answers
6k
views
Method not found: 'System.Reflection.MethodInfo Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.get_SelectAsyncMethod()'
People I need help I am in a new project in which I am implementing identity server 4 and I am trying to recover the previously created users with asp.Identity that I have in my database to be able to ...
24
votes
2
answers
6k
views
EF Core Second level ThenInclude missworks
Assume having these models first:
Method that has one OriginalCode
OriginalCode that has many Mutants
Mutant that has many ParseSubTrees
Now when querying on Method I want the other being loaded. So ...