All Questions
Tagged with .net-core entity-framework
1,928
questions
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....
117
votes
39
answers
259k
views
EF Core add-migration Build Failed
I have a developer that is getting "Build failed." when running add-migration in a .NET Core EF project, with no explanation of why the build failed. How do you troubleshoot this error?
This is what ...
115
votes
14
answers
115k
views
Cannot find command 'dotnet ef'
I am using .NET Core 2.0 on Arch Linux / Visual Studio Code and am trying to get EF tools to work, but I keep getting the error:
cannot find command dotnet ef
I've just about looked everywhere and ...
95
votes
9
answers
80k
views
Suppress SQL Queries logging in Entity Framework core
I have a console .net core app that uses entity framework core.
The app uses logging framework to write to file and console:
serviceProvider = new ServiceCollection()
.AddLogging()
....
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 ...
82
votes
10
answers
293k
views
instance of entity type cannot be tracked because another instance with same key value is tracked [closed]
I'm using generic repository pattern in asp.net core 2.0 which can not dispose repository object, when I am going to update the entry its updated for one time successfully but when I am trying to ...
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 ...
60
votes
2
answers
35k
views
ASP.NET EntityFramework get database name
I created an ASP.NET EF application with MySQL using the following tutorial: http://www.asp.net/identity/overview/getting-started/aspnet-identity-using-mysql-storage-with-an-entityframework-mysql-...
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?....
54
votes
2
answers
44k
views
What's the difference between <TargetFramework> and <RuntimeFrameworkVersion>?
I have the following code in a csproj file:
<TargetFramework>netcoreapp1.0</TargetFramework>
In the NuGet package manager, it says that I have Microsoft.NETCore.App version 1.0.5
Now let'...
53
votes
1
answer
41k
views
Are there DBSet.UpdateAsync() and RemoveAsync() in .net core?
I could not find any info on this anywhere.
There are ToListAsync(), AddAsync() and more, but could not find any documentation about UpdateAsync() or RemoveAsync().
Does anyone know anything about ...
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 (...
43
votes
2
answers
24k
views
What is the use of Normalized Email & UserName in .NET core IdentityUser Model?
When I use IdentityUser model in Asp.Net Identity with EntityFramework, it creates some standard fields in the database. All the fields are self explanatory except for the below two fields.
...
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.
...
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
...
40
votes
3
answers
25k
views
Can I use Entity Framework 6 (not core) in .net core?
Entity Framework Core does not support spatial data, which I need to use in my app.
Can I use Entity Framework 6 in .net core? If so, how can I register DatabaseContext in Startup.cs?
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
4
answers
50k
views
LocalDB is not supported on this Platform
I'm trying to launch .Net Core 2.0 application on Ubuntu 17.04. I developed it on Windows 10 before and it works well. The problem is that when I run dotnet ef database update I get the next exception:...
34
votes
3
answers
120k
views
The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation
I have C# application (.NET Core 3.1) and I have written the following LINQ expression.
public ActionResult<bool> GetItems(string title)
{
var items = _service.All.GetItems()....
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
21
answers
53k
views
Unable to create an object of type 'MyContext'. For the different patterns supported at design time
I have ConsoleApplication on .NET Core and also I added my DbContext to dependencies, but howewer I have an error:
Unable to create an object of type 'MyContext'. For the different patterns supported ...
31
votes
3
answers
27k
views
Change or rename a column name without losing data with Entity Framework Core 2.0
I realised that I had spelt one of my column headers incorrectly so I changed it in the model and created a new migration to update it into the database. All worked perfectly until I realised that ...
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
7
answers
65k
views
Unit testing with EF Core and in memory database
I am using ASP.NET Core 2.2, EF Core and MOQ. As you can see in the following code, I have two tests, and running both together, with both database name "MovieListDatabase" I got an error in one of ...
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
1
answer
12k
views
Equivalent of SqlFunctions in EF Core
What is the equivalent of SqlFunctions in Entity Framework (EF) Core 2.0?
I am trying to convert this to EF Core
private static readonly MethodInfo StringConvertMethodDouble = typeof(SqlFunctions)....
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
2
answers
28k
views
How to perform a group join in .NET Core 3.0 Entity Framework?
With the changes to .NET Core 3.0 I am getting
... NavigationExpandingExpressionVisitor' failed. This may indicate
either a bug or a limitation in EF Core. See
https://go.microsoft.com/fwlink/?...
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 ...
24
votes
2
answers
40k
views
How to execute SqlQuery with Entity Framework Core 2.1?
In Entity Framework 6, I can execute a raw SQL query on the database using the following command:
IEnumerable<string> Contact.Database.SqlQuery<string>("SELECT a.title FROM a JOIN b ON b....
24
votes
3
answers
24k
views
EF core DbContext in a multithreaded API application
tl;dr How can I use Entity Framework in a multithreaded .NET Core API application even though DbContext is not threadsafe?
Context
I am working on a .NET Core API app exposing several RESTful ...
22
votes
5
answers
36k
views
Using Entity Framework Core migrations for class library project
This seem to be an issue that have been fixed already, at least for the SQLite databases.
My solution consists of 3 projects:
WPF project (default startup project) (.NET Framework 4.7),
"Core" ...
22
votes
4
answers
11k
views
Recommended way to clean old Entity Framework Core migrations
After developing our application for a while we've accumulated quite a bit of EFCore database migrations. Since EFCore adds a snapshot of the entire db model to every migration, this code adds up ...
22
votes
2
answers
12k
views
Injecting Env Conn String into .NET Core 2.0 w/EF Core DbContext in different class lib than Startup prj & implementing IDesignTimeDbContextFactory
I honestly cannot believe how hard this is...first off the requirements that I am going for:
Implementing Entity Framework Core 2.0' IDesignTimeDbContextFactory which is IDbContextFactory renamed to ...
21
votes
1
answer
18k
views
WithOptional with Entity Framework Core
I'm trying to migrate my old app to the new EF Core but I cannot find some relationships like:
HasRequired(o => o.Document).WithOptional(o => o.CancelNote);
Is there some extension methods? ...
21
votes
3
answers
10k
views
IDbAsyncQueryProvider in EntityFrameworkCore
I'm using XUNIT to test in a dot net core application.
I need to test a service that is internally making an async query on a DbSet in my datacontext.
I've seen here that mocking that DbSet ...
19
votes
3
answers
7k
views
Cannot use 'dotnet ef...' - The specified framework version '2.0' could not be parsed
My project builds without any issues and can run without issues, but I cannot use dotnet ef migrations because of this strange error:
The specified framework version '2.0' could not be parsed
The ...
18
votes
3
answers
18k
views
How to make lazy-loading work with EF Core 2.1.0 and proxies
I have the following models:
public class Session
{
public int SessionID { get; set; }
public int UserID { get; set; }
public virtual User User { get; set; }
}
public class User
{
...
18
votes
4
answers
21k
views
EF Core tools version update 2.1.1
If I run dotnet ef add testmigration
I get this warning: The EF Core tools version '2.1.0-rtm-30799' is older than that of the runtime '2.1.1-rtm-30846'. Update the tools for the latest features and ...
17
votes
2
answers
12k
views
.NET Core EF, cleaning up SqlConnection.CreateCommand
I am using .NET Core DI to get DbContext and in my logic I need to execute raw SQL commands also on DB, so for that purpose I am creating DbCommand to execute SQL like this(just an example query, ...
17
votes
4
answers
9k
views
EF Core - Running migrations without sources - Equivalent of EF6's migrate.exe
Is it possible to run an ef migration from DLL containing migrations and dbcontext? I'd like to run dotnet ef database update against my build artefacts without need of project.json and source codes.
...
17
votes
3
answers
13k
views
The entity type 'List<PersonAddress>' was not found. Ensure that the entity type has been added to the model
I'm building an application with .net core 2.0. I have a Person object with a list of addresses. Addresses are stored in the Addresses table. I then have a joining table that relates Person to ...
16
votes
2
answers
13k
views
EF Core linq and conditional include and theninclude problem
I am having a problem with getting a result when trying to get objects with multiple levels.
This is what I am trying to do roughly:
_context.Investors.Where(s => s.Id == userId)
.Include(c =&...
16
votes
3
answers
12k
views
Entity Framework Core 3 raw SQL missing methods
I'm trying to use EF Core 3 to delete all rows from a table like:
db.MyTable.ExecuteSqlRaw("delete from MyTable;");
But I get the error:
DbSet' does not contain a definition for 'ExecuteSqlRaw' ...
16
votes
2
answers
19k
views
Include / ThenInclude with where in EF Core [duplicate]
I need to use where in ThenInclude
var templatesFields = await _context.Sections
.Include(x => x.Subtitles)
.ThenInclude(r => r.Fields.Where(t=>t.TemplatesFields.TemplateID==TemplateID))
....
16
votes
4
answers
33k
views
Using parameters with EntityFramework and `FromSql`
public List<PostJobListModel> GetPostsByCompanyId(int id, int s, int d, int p)
{
string command = @"select Id,Title,Cities = STUFF(
(SELECT ',' + City.Name
FROM City where ...