Questions tagged [entity-framework-core]

1

votes
0

answer
6

Views

change entity framework core code first migration name format

ef core using system calendar format for generating migration names. example of standard migration name for the Gregorian calendar on windows: 20190206144020_MIGRATION-NAME But if the date format of Windows is something other than the Gregorian, like Persian calendar the ef core migration name gene...
AliReza
0

votes
0

answer
12

Views

Force inner join from EF Cores Include() on optional relationship

We changed a relationship from required to optional, now the resulting SQL produced by EF Core's Include() does a left outer join rather than inner join. Problem is that those optional entities have query filters on them that is required. Let's say we have the following; public class First { public...
HenrikM
0

votes
0

answer
6

Views

Linq, EF Core - group by on one field and use other field to get list of data from other table

Can someone help me. I tried everything, nothing worked. I have data in this format table1 Id T_Id 4 2 6 5 6 7 table2 T_Id fname lname 2 mary smith 5 john pope 7 steve blair I want to get data in following format using linq query with EF Core. [{ 'Id' : '4', 'users':[{ 'T_I...
Shamal
1

votes
1

answer
65

Views

ASP.NET Core DI with Simple Injector sharing DbContext

I'm setting up an ASP.NET Core project and following a CQRS pattern basing my work on the Tripod project. I've followed the Simple Injector integration guide but a little puzzled on one point... I want to keep make use of the UseInMemoryDatabase option for testing and can only find examples of that...
Stephen York
1

votes
2

answer
77

Views

Entity Framework Core SQLite Connection String Keyword not supported: version

I created a ASP.NET MVC website using .NET Core 2.2 using a SQLite database. So far it's working well. Trouble begins when I want to add SQLite-specific keywords to the connection string, such as Data Source=~\\App_Data\\MyDb.db; Version=3; DateTimeFormat=UnixEpoch; DateTimeKind=Utc Now I get Keywor...
Etienne Charland
1

votes
1

answer
83

Views

EF Core with Mysql not scaffolding BIT(1) to Boolean

I am trying to generate DB classes from the existing database (DB first) through. Net core CLI. The DB was perfectly working with EF5 and .net4.5 but after migrate to EF core the bit(1) field is not scaffolding as bool instead it is generating as short. Also tried with TinyInt(1) but no luck. Techno...
ni3.net
1

votes
1

answer
133

Views

Microsoft.Extensions.EntityFrameworkCore namespace not found in .NET Core 2.2

I'm reading the book 'Pro ASP.NET Core MVC 2' and following his samples, and have reached the point where he introduces EF Core. He says to add the following to the .csproj file... ...and then add the following to the ConfigureServices method in Startup.cs... services.AddDbContext(options => options...
Avrohom Yisroel
1

votes
1

answer
32

Views

Move DbSet<TEntity> properties to a Separate Class in Entity Framework Core

Here is what I have currently in MyContext class public class MyContext:DbContext { public DbSet Countries { get; set; } public DbSet States { get; set; } } What I want is to place all these DbSet properties into a separate file and just have one statement on MyContext class to add all...
Bipn Paul
1

votes
2

answer
41

Views

How to use Entity Framework in-memory database in an F#?

I am trying to use Entity Framework Core with F# with the in memory database for a very simple use case: open System open Microsoft.EntityFrameworkCore type Position = { X: double Y: double } type Airport = { Id: Guid Name: string Position: Position } type MyContext = inherit DbContext new() = { inh...
Ehouarn Perret
1

votes
1

answer
52

Views

How do I do an async query without enumerating the data?

I'm trying to work out how to write a generic async GetAll database method in Entity Framework Core without enumerating the data. I want GetAll to be async, but don't want it to enumerate the data, so that I can add a Where method afterwards, and have that applied to the database query.. List custom...
Avrohom Yisroel
1

votes
3

answer
39

Views

edit only changed or mentionned values with entity framework core

I need to update only mentioned fields in the put request body , the current issue is that all the values that are not mentioned in the entity to update are set to null below is my currrent update implementation in the generic repository. public virtual void Update(T entity) { Context.Attach(entity...
Jawhar Jarrar
0

votes
1

answer
13

Views

LINQ with Include and Where not showing desired results

I have a linq statement that combines Residents and Requests table like so var resident = await _context.Resident .Include(s => s.Requests) .FirstOrDefaultAsync(m => m.ID == id); The problem is that all requests show up even those that are not related to the Resident. I have tried ad...
Podge Dimagiba
0

votes
2

answer
11

Views

Model's indexes doesn't validate

Table in postgres: CREATE TABLE 'public'.'filters' ( 'Id' int4 NOT NULL DEFAULT nextval(''filters_Id_seq''::regclass), 'Name' varchar(200) COLLATE 'pg_catalog'.'default' NOT NULL DEFAULT ''::character varying, 'Type' int4 NOT NULL, 'Alias' varchar(200) COLLATE 'pg_catalog'.'default' NOT NULL DEFAULT...
TrueGameover
0

votes
0

answer
3

Views

Entity Framework Core's AddRangeAsync() doesn't work in a generic method

This works: await AddRangeAsync(myEntities); But this doesn't work: await AddEntities(myEntities); ...when I try to use this function: private async Task AddEntities(IEnumerable entities) { await AddRangeAsync(entities); } I get an error: 'The entity type 'List' was not found. Ensure that the entity...
Chris
1

votes
2

answer
1.6k

Views

Do I use Snapshot file, migration file or data annotations in my EF Core to update database?

I'm trying to understand the different types of migration paths we can choose when developing an ASP.NET Core 1.0 application with EF Core. When I created my first Core application I noticed it generated a ApplicationDbContextModelSnapshot class that uses a ModelBuilder to build the model. Then I r...
Ray
0

votes
0

answer
12

Views

Convert Dictionary to ICollection in Entity Framework Core

Here I am stucked with the conversion of dictionary to Icollection in EF Core. I have Dictionary in FlatEmployee class in which I am storing list of key, value pair in database. I have declared like this: public class FlatEmployee { public int EmployeeId { get; set; } public Dictionary PayAndAllowan...
Iswar
1

votes
1

answer
84

Views

How to properly use IPluralizer in IDesignTimeServices

In my app I use code-first approach and I'm adding entities to DbContext only via IEntityTypeConfiguration. My goal is to achieve pluralized table names i.e. Models for Model. After reading documentation, article, This question my understading would be that my pluralizer should be registered and as...
WueF
1

votes
1

answer
39

Views

How do I change the ILazyLoader implementation in Entity Framework Core 2.2?

I created an ASP.NET Core 2.2 Application, in Startup.cs I have this: services.AddEntityFrameworkSqlServer(); services.AddScoped(); services.AddDbContext(builder => { builder.UseLazyLoadingProxies(); builder.UseSqlServer(Configuration.GetConnectionString('DefaultConnection')); var options = builder....
Luiz Bicalho
1

votes
2

answer
37

Views

Writing One to one relationship using Fluent API

How to write a one-to-one--or-zero relationship using fluent api? Can someone help me out correct what i have written. I am not sure if its correct. The scenario : One Student Can have one or zero Address. Student Model public int Id{ get; set; } public string Name{ get; set; } public Address Addres...
Illep
1

votes
1

answer
38

Views

EF Core querying data by related IDs

Suppose I have two classes that have a many-to-one relationship public class ParentObject{ public int Id { get; set; } public List Children { get; set; } ... } public class ChildObject{ public int Id { get; set; } public ParentObject Parent { get; set; } } When I add the migration and update the dat...
gregsdennis
1

votes
1

answer
67

Views

Why does Linq To Entities create query that is not optimized

There is a query that is created by EF that uses an Index Scan, instead of an Index Seek. By modifying the query slightly to not use a parameter, an index seek is used instead. The index scan is about three seconds, whereas the Seek is instant. Generated Query (uses Index Scan): exec sp_executesql...
Greg Gum
1

votes
1

answer
57

Views

EF Core dealing with alternate primary keys

I have some EF Core models that were built using dotnet ef dbContext scaffold to produce models using a database first approach. My problem is the database uses an integer primary key, used for linking tables together but has a string based index that would be used as the sane index for searching th...
Chris Becke
1

votes
1

answer
86

Views

Circular dependency in ASP.NET Core

For school we have to write our own WebApi using the .NET Entity Core Framework. I've written my api but when I tried to use it in swagger, it always returned a HTTP 500 error: internal server error. I downloaded Fiddler to start debugging and came across a circular dependency error in my repository...
Mout Pessemier
0

votes
0

answer
5

Views

How to create one-to-ne connection between primary key and other key(Entity Framework Core)

I have two table. Table One have primary keys and key sido,table Two have sido key but this is primary key when I use model builder to connect throw exception - The relationship from 'Fbdk.LBg' to 'LBg.Fbdk' with foreign key properties {'SidO' : decimal} cannot target the primary key {'SidO' : decim...
1

votes
1

answer
110

Views

LINQ query to find related data [duplicate]

This question already has an answer here: Projecting self referencing multi level Entities In Entity Framework 6 2 answers I got some help with my recursive product category tree view here on Stack Overflow before, and this is working: Entity model: public class ProductCategory { public int Id { g...
Stian
1

votes
1

answer
2.1k

Views

Change tracking in Entity Framework Core 2.0 changed my code behavior

That code worked fine in Entity Framework 1.0 could also be 1.1. var pupilFound = await context.Pupils.SingleOrDefaultAsync(p => p.Id == pupil.Id); if (pupilFound == null) { throw new BadDataException($'{nameof(pupil.Id)} is not valid'); } if (pupilFound.UserId != userId) { throw new NotAuthorizedEx...
HelloWorld
1

votes
0

answer
67

Views

Refresh user's permissions after removed from a role

I have a project where I can assign users to roles. My problem is that when I remove a user from a role, the user will be able to access the resources that he is no loger supposed to see. The only way to refresh his permissions is when he log off and login again. This is a problem because when I wan...
pitaridis
1

votes
1

answer
233

Views

Structuremap - dependency injection - EF Core DBContext lifetime per request

Has anyone had any luck setting up an EF Core DBContext with Structuremap 'correctly' (what is correctly?) DBContext needs to be a singleton across the lifetime of the request. I understand that the default lifecycle is Transient. I understand that that will get a nested container, which effectively...
jenson-button-event
1

votes
2

answer
591

Views

How to remove circular reference from entity framework core

Web Api Core, Entity Framework Core Situation - Many to many relationship. A, Join, B. Join has Id references to both A and B. A has collection for Joins. B has collection for Joins. Goal - Get A with Joins with B - and no further. Problem - I get A with Joins with B with the same singular Join...
user1904662
1

votes
1

answer
65

Views

Only two properties of the model are updated

I defined entity presented below: public class Customer { public int CustomerId { get; set; } [Required] public string FirstName { get; set; } [Required] public string LastName { get; set; } [Required] public string PhoneNumber { get; set; } [Required] public string Email { get; set; } public IColle...
pw94
1

votes
1

answer
222

Views

ASP.net core identity 2.0: proper way to use claims identity with db lookup?

I'm new to the concept of claims-based identity so if the question is unclear, please let me know. I've got a derived user collection, and it relates to an 'organizations' collection. A user can be an admin in related organizations. I'm trying to build the ability to have an [IsAdminForThisOrg] attr...
BenjiFB
1

votes
0

answer
170

Views

EF Core Include with FromSql extension without sub SELECT

I have some query to get companies from database like this: var fullTextSearchVal = 'test'; var sql = $'SELECT * FROM Companies WHERE CONTAINS(*, '\'*{fullTextSearchVal}*\'')'; var result = _context.Companies.Include(i => i.Address).FromSql(sql).ToList(); It works well, but have a little problem aft...
1

votes
1

answer
513

Views

Could not load file or assembly '<project name>, Culture=neutral, PublicKeyToken=null'. Anyone suggest a fix?

I have three Dotnet Core 2.0 projects; Angular, Domain and EF7. Domain and EF7 are .NETStandard libraries, Angular is the Core 2.0 Angular web project template. Please note that despite the project name, EF7 is using Entity Framework Core 2.01, not Entity Framework 7. I tried to run migrations today...
benanderson89
1

votes
2

answer
356

Views

ASP.Net Core - EntityFrameworkCore data is not adding, updating instead

I am using ASP.Net Core 2 and Entity Framework Core with MySQL. I want to add a simple entity to the database. My Model is like this- public class Employee { public int Id { get; set; } [Required] public string Name { get; set; } [Required] public string City { get; set; } [Required] public string D...
IT Eng - BU
1

votes
0

answer
1.2k

Views

Could not load type 'Microsoft.EntityFrameworkCore.Infrastructure.DesignTimeProviderServicesAttribute'

I use Visual Studio for Mac, created a .net core web api project. The csproj file is like this: netcoreapp2.0 When I execute 'dotnet ef dbcontext scaffold' to scaffold from MySQL with the following command: dotnet ef dbcontext scaffold 'server=[server];port=3306;user=[user];password=[password];datab...
Ternence.Lin
1

votes
0

answer
207

Views

Error when filtering on an Include in Entity Framework Core

I have the following EF Core linq statement: var contact = dataContext.Contacts .Include(c => c.Addresses) .ThenInclude(a => a.AddressType) .Where(c => c.ContactId == contactId) .Select(c => new Contact() { ContactId = c.ContactId, Title = c.Title, FirstName = c.FirstName, Surname = c.Surname, Addre...
Carl Rippon
17

votes
1

answer
8.5k

Views

EF Core returns null relations until direct access

I have some models like those below: public class Mutant { public long Id { get; set; } ... // Relations public long OriginalCodeId { get; set; } public virtual OriginalCode OriginalCode { get; set; } public int DifficultyLevelId { get; set; } public virtual DifficultyLevel DifficultyLevel { get; se...
ConductedClever
1

votes
0

answer
252

Views

Get a list of Entity Framework validation errors

I'm building an Asp.net Core 2 MVC application that references a vendor's database. While we have attempted to ensure that the required data elements for the application are present, inevitably some data-quality issues will ensue (their application's rules don't require some fields that we plan to...
craig
1

votes
1

answer
500

Views

Entity Framework Core can not add migration

Executing dotnet ef add migration InitialMigration command giving this error A suitable constructor for type 'Vega.Repository.VegaContext' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor. My ConfigureServices method on Startup...
Towhid
1

votes
0

answer
55

Views

Entity Framework Core. How to map two collections to one column?

I have following model, which I want to manage with Entity Framework Core: public class Product { public int ID { get; set; } public string Name { get; set; } public int Price { get; set; } } public class ProductsReport { public int ID { get; set; } public string Author { get; set; } public string R...
Dimitry

View additional questions