entity-framework-core

How to include only selected properties on related entities

我只是一个虾纸丫 提交于 2019-12-01 22:42:06
问题 I can include only related entities. using (var context = new BloggingContext()) { // Load all blogs, all related posts var blogs1 = context.Blogs .Include(b => b.Posts) .ToList(); } However, I don't need entire BlogPost entity. I'm interested only in particular properties, e.g: using (var context = new BloggingContext()) { // Load all blogs, all and titles of related posts var blogs2 = context.Blogs .Include(b => b.Posts.Select(p => p.Title) //throws runtime exeption .ToList(); foreach(var

How to detect when EF Core must do some of the IQueryable operations in memory

自作多情 提交于 2019-12-01 22:16:47
I've been going through my application, and there are times where only part of the IQueryable is actually translated into a SQL query and the rest of the work is done in-memory. I understand that there's no way for the EF team to account for every possible expression that a developer may come up with and magically translate that into a useable SQL query, but IIRC, EF would throw an exception if it was unable to translate ALL of the operations defined in an IQueryable to SQL. Is there a way to have EF Core also throw an exception, or at the very least, raise an event when it's unable to fully

Getting DbContext when resolving a singleton

喜夏-厌秋 提交于 2019-12-01 21:44:19
Within ConfigureServices I have services.AddDbContext<MyContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); as well as services.AddSingleton<IMyModel>(s => { var dbContext = s.GetService<MyContext>(); var lastItem= dbContext.Items.LastOrDefault(); return new MyModel(lastItem); }); But s.GetService<MyContext>() throws an error: Cannot resolve scoped service 'MyContext' from root provider. How can I achieve that? I don't want to inject MyDbContext in MyModel contructor as it is in a library which should have no reason to know about Entity Framework

Use a Inline Table-Valued Functions with Linq and Entity Framework

白昼怎懂夜的黑 提交于 2019-12-01 21:26:55
I created an Inline Table-Valued Functions (ITVF) in SQL Server that returns a table of values (query simplified for discussion purposes): CREATE FUNCTION dbo.VehicleRepairStatus() RETURNS TABLE AS RETURN SELECT VehicleID, CurrentStatus FROM VehicleRepairHistory ... Which I can reference in a query: SELECT v.ID, v.Name, r.CurrentStatus FROM Vehicle v LEFT OUTER JOIN dbo.VehicleRepairStatus() r on v.ID = r.VehicleID I'd like to be able to use it in Linq query: var vehicles = await _databaseContext.Vehicles .Join() // join ITVF here? .Where(v => v.Type == 'Bus' ) .OrderBy(v => v.Name)

Automapper ProjectTo adds ToList into child properties

梦想与她 提交于 2019-12-01 21:18:28
I use projection to map the Entity classes to DTOs using Entity Framework Core. However, projection adds ToList into child collection properties and this slows down the query a lot. Company Entity: public class Company { public Company() { Employees = new List<CompanyEmployee>(); } public string Address { get; set; } public virtual ICollection<CompanyEmployee> Employees { get; set; } ... } Company DTO: public class CompanyDTO { public CompanyDTO() { CompanyEmployees = new List<EmployeeDTO>(); } public string Address { get; set; } public List<EmployeeDTO> CompanyEmployees { get; set; } ... }

EF Core 2.0.0 One to One-or-Zero with Fluent Api

早过忘川 提交于 2019-12-01 19:23:11
In Fluent Api at EF Core 2.0.0 , there aren't any methods HasRequired and HasOptional , and i have tow Models, Person and Employee: public class Person { public int Id { get; set; } public int EmployeeId { get; set; } public virtual Employee Employee { get; set; } // Optional } public class Employee { public int Id { get; set; } public int PersonId { get; set; } public virtual Person Person {get; set; } // Required } Person May to have Employee: Optional Employee Should have Person: Required How to apply these convetions in database. You could just specify int? as EmployeeId property type. BTW

EF Core Include on multiple sub-level collections

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 19:20:52
Consider this aggregate root... class Contact { ICollection<ContactAddress> Addresses { get; set; } ICollection<ContactItem> Items { get; set; } ICollection<ContactEvent> Events { get; set; } } ...which I have used like so... class Person { Contact ContactDetails { get; set; } } How do I eager load all of the collections with the contact? I tried this... Context .Set<Person>() .Include(o => o.ContactDetails) .ThenInclude(o => o.Addresses) .ThenInclude(????) . ... I've also tried this... Context .Set<Business>() .Include(o => o.ContactDetails.Addresses) .Include(o => o.ContactDetails.Events)

Custom Execution Strategy Error in VS2017 and Entity framework Core 2.0

廉价感情. 提交于 2019-12-01 18:57:43
I'm reading "Building Web Applications with Visual Studio 2017" (by Philip Japikse, Kevin Grossnicklaus, and Ben Dewey) and am getting stuck. When trying to create a class for a custome execution strategy with Entity Framework Core I get error CR0246 "The type or namespace name 'ExecutionStrategyContext' could not be found (are you missing a using directive or assembly reference?)" The text only states that only System and Microsoft.EntityFrameworkCore.Storage are required references. The EF Core 2.0 documentation seems to match the text but I cannot get the error to go away. Note: The book

Entity framework 7 in ASP.NET MVC6 multiple foreign key to the same table

妖精的绣舞 提交于 2019-12-01 18:14:21
问题 Hi I have the same problem that an old post that is here, the solution offer there doesn't work for me in MVC 6 with EF7 is simple public class Match { [Key] public int MatchId { get; set; } public DateTime playday { get; set; } public float HomePoints { get; set; } public float GuestPoints { get; set; } public int HomeTeamId { get; set; } public int GuestTeamId { get; set; } [ForeignKey("HomeTeamId")] [InverseProperty("HomeMatches")] public virtual Team HomeTeam { get; set; } [ForeignKey(

Entity framework 7 in ASP.NET MVC6 multiple foreign key to the same table

一笑奈何 提交于 2019-12-01 18:10:13
Hi I have the same problem that an old post that is here, the solution offer there doesn't work for me in MVC 6 with EF7 is simple public class Match { [Key] public int MatchId { get; set; } public DateTime playday { get; set; } public float HomePoints { get; set; } public float GuestPoints { get; set; } public int HomeTeamId { get; set; } public int GuestTeamId { get; set; } [ForeignKey("HomeTeamId")] [InverseProperty("HomeMatches")] public virtual Team HomeTeam { get; set; } [ForeignKey("GuestTeamId")] [InverseProperty("AwayMatches")] public virtual Team GuestTeam { get; set; } } public