entity-framework-core

How to inject DbContext instance in the ConfigureServices method of startup.cs correctly (ASP.net core 1.1)?

我是研究僧i 提交于 2021-01-27 07:03:04
问题 I have implemented the EntityFrameworkFileProvider for my ASP.NET core web application, I want the ViewDbContext instance to be injected by ASP.NET core DI framework in the constructor: ( ViewDbContext is a dbContext ) public class EntityFrameworkFileProvider : IFileProvider { private ViewDbContext _context; public EntityFrameworkFileProvider(ViewDbContext context) { /* should be injected by asp.net core DI */ _context = context; } public IDirectoryContents GetDirectoryContents(string subpath

EF Core “Invalid column name 'Discriminator'” error with inheritance

笑着哭i 提交于 2021-01-27 06:44:08
问题 I have the following entities: public abstract class Freezer { public int FreezerId { get; set; } public string Name { get; set; } public int FreezerTypeId { get; set; } public int Capacity { get; set; } } public class FoodFreezer : Freezer { public List<FoodItem> Foods { get; set; } } public class ChemicalFreezer : Freezer { public List<ChemicalItem> Chemicals { get; set; } } As you can see the derived Freezer classes do not contain any data that would be stored in the database; they only

EF Core “Invalid column name 'Discriminator'” error with inheritance

丶灬走出姿态 提交于 2021-01-27 06:41:53
问题 I have the following entities: public abstract class Freezer { public int FreezerId { get; set; } public string Name { get; set; } public int FreezerTypeId { get; set; } public int Capacity { get; set; } } public class FoodFreezer : Freezer { public List<FoodItem> Foods { get; set; } } public class ChemicalFreezer : Freezer { public List<ChemicalItem> Chemicals { get; set; } } As you can see the derived Freezer classes do not contain any data that would be stored in the database; they only

Handling MySQL Zero Date with EF Core

可紊 提交于 2021-01-27 06:36:26
问题 Background: I have 200+ legacy VB6 apps which I am converting to C#, using EntityFramework Core as our ORM. Unfortunately a lot of the apps utilize MySQL's zero date (0000-00-00) . So I need to cater for this and be able to store a zero date in some way. Changing the fundamental way this works in the 200+ apps is not currently an option. Setup: I can define an entity property which represents the field definition in MySQL eg: public DateTime ExpiryDate { get; set; } ... entity.Property(e => e

Nullable Owned types in EF Core

左心房为你撑大大i 提交于 2021-01-27 06:22:07
问题 I my case I want to store an address but it has to be optional. My mapping lookes like this: map.OwnsOne(x => x.Address, cb => cb.OwnsOne(l => l.Location)); But when comitting my DbContext with Address as null iam getting this error: InvalidOperationException: The entity of 'Member' is sharing the table 'Members' with 'Member.Address#StreetAddress', but there is no entity of this type with the same key value 'Id:-2147480644' that has been marked as 'Added'. I then instantiated the Address and

ThenInclude not working for an Entity Framework LINQ query [duplicate]

≡放荡痞女 提交于 2021-01-26 09:16:51
问题 This question already has an answer here : Can not load related data with Include or ThenInclude or Select/Many with ONE query [duplicate] (1 answer) Closed 3 years ago . I have a database model like this: public class Customer { public int CustomerId{ get; set; } public int OrderId { get; set; } public ICollection<Order> Orders { get; set; } } public class Order { public int OrderId { get; set; } public int Amount { get; set; } public int ProductId { get; set; } public Product Product { get;

ThenInclude not working for an Entity Framework LINQ query [duplicate]

微笑、不失礼 提交于 2021-01-26 09:13:32
问题 This question already has an answer here : Can not load related data with Include or ThenInclude or Select/Many with ONE query [duplicate] (1 answer) Closed 3 years ago . I have a database model like this: public class Customer { public int CustomerId{ get; set; } public int OrderId { get; set; } public ICollection<Order> Orders { get; set; } } public class Order { public int OrderId { get; set; } public int Amount { get; set; } public int ProductId { get; set; } public Product Product { get;

How to use GroupBy in an asynchronous manner in EF Core 3.1?

冷暖自知 提交于 2021-01-26 04:11:13
问题 When I use GroupBy as part of a LINQ query to EFCore, I get the error System.InvalidOperationException: Client-side GroupBy is not supported . This is because EF Core 3.1 attempts to evaluate queries on the server-side as much as possible, as opposed to evaluating them on the client-side, and the call cannot be translated to SQL. So the following statement does not work, and produces the error mentioned above: var blogs = await context.Blogs .Where(blog => blog.Url.Contains("dotnet"))

Repeated Update causes tracking error

£可爱£侵袭症+ 提交于 2021-01-24 08:28:42
问题 Im using VisualStudio 2017 with .Net Core 2 and EntityFrameworkCore (v2.0.1). The application is a console application which starts an MQTT Client and then processes the received messages, storing them into a database. Each time the application starts and on the first update it works as expected. However on each subsequent update with the same data (none, one or more fields are changed), it throws an System.InvalidOperationException with the following message: The instance of entity type

Is it possible to automatically map a DB view on Entity Framework Core version 2.1?

大兔子大兔子 提交于 2021-01-23 11:10:38
问题 I found this interesting question on stackoverflow about mapping a view and working with it in EF Core. According to its answer, with previous EF Core versions wasn't possible to automatically map views. But what now? According to the Entity Framework 2.1 Roadmap, An EF Core model can now include query types. Unlike entity types, query types do not have keys defined on them and cannot be inserted, deleted or updated (i.e. they are read-only), but they can be returned directly by queries. Some