entity-framework-core

Problem with EF OrderBy after migration to .net core 3.1

一个人想着一个人 提交于 2020-02-02 05:20:55
问题 Consider this code: _dbContext.Messages .GroupBy(m => new { MinId = m.SenderId <= m.RecipientId ? m.SenderId : m.RecipientId, MaxId = m.SenderId > m.RecipientId ? m.SenderId : m.RecipientId }) .Select(gm => gm.OrderByDescending(m => m.SentAt).FirstOrDefault()); By this I group all dialogues of users by their Id's no matter who sent the message. Then I order messages by SentAt date inside the groups and select one last message out of each dialogue. The thing is that this code worked and more

Problem with EF OrderBy after migration to .net core 3.1

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-02 05:20:28
问题 Consider this code: _dbContext.Messages .GroupBy(m => new { MinId = m.SenderId <= m.RecipientId ? m.SenderId : m.RecipientId, MaxId = m.SenderId > m.RecipientId ? m.SenderId : m.RecipientId }) .Select(gm => gm.OrderByDescending(m => m.SentAt).FirstOrDefault()); By this I group all dialogues of users by their Id's no matter who sent the message. Then I order messages by SentAt date inside the groups and select one last message out of each dialogue. The thing is that this code worked and more

Are there Database Initializers in Entity Framework Core?

对着背影说爱祢 提交于 2020-02-02 00:30:32
问题 I can't find examples on how to use EF Database Initializers with EF Core nor can I find a list of EF Database Initializers shipping with EF Core. Has the concept of Database Initializers become obsolete? What's the current approach of initializing a database ... or not initializing an existing database? 回答1: In .Net core you can use functions of Database property form DbContext instance. It allows you to create database (EnsureCreated), destroy database (EnsureDeleted) and migrate to last

EntityFramework Core relation to composite key

强颜欢笑 提交于 2020-02-01 04:09:27
问题 Consider the following database tables. Unfortunately the tables cannot be altered in any way. Houses has an auto-increment ID field named Id , a string field named Name and an integer field named AreaId . The latter is not a foreign key to the Areas table. Areas has a composite key consisting of AreaId , CountryId and LangId . An Area with the same AreaId can exist but with different CountryId and LangId . E.g.: There can be two rows with the same AreaId but different LangId . NOTE: Why does

generic methods for API controller

☆樱花仙子☆ 提交于 2020-01-30 05:44:29
问题 I'm writing an API for my game and I'm starting to realize that the amount of GET, POST, and PUT API methods can really add up. So right now, I'm trying to make it more generic so that I don't have to write a separate method like GetMonsterList, GetTreasureList, GetPlayerInfo, etc. But I'm not quite sure how to go about doing that. Here is a non-generic PUT method that I currently have. // PUT: api/MonsterLists/5 [HttpPut("{id}")] public async Task<IActionResult> PutMonsterList(string id,

ASP.NET Core and EF Core 1.1 - Display data using Stored Procedure

£可爱£侵袭症+ 提交于 2020-01-29 05:44:42
问题 I have problem with my query. I want to display the result on a view. [HttpGet] [ValidateAntiForgeryToken] public async Task<IActionResult> Index() { return View(await _Context.Employee .FromSql("EXEC sp_GetLoanDetails") .ToArrayAsync()); } Here is my list of items I want to view: public class StoredProcRow { [Key] public int empID { get; set; } public string empFullName { get; set; } public double EducationalLoan { get; set; } public double PettyCash { get; set; } public double BusinessLoan

Entity Framework Core: many-to-many relationship between generic object tables

守給你的承諾、 提交于 2020-01-25 06:44:25
问题 I have a Cat and Dog table and I want to create many-to-many relationships to an Image table. I don't want to create a join table for every table (DogImage, CatImage, BirdImage, etc) so I figured I would create a single EntityImage join table and then use a Type field to know what type of image it is. Here is my attempt at the model below, however this creates CatId, DogId, etc foreign keys in the EntityImage table, instead of using the EntityId I tried to define. Does anyone know how to

.ThenInclude for sub entity in Entity Framework Core 2

放肆的年华 提交于 2020-01-24 21:27:20
问题 Previously (when using .net 4.5.2 and EF 6). I have had a generic Get method that accepted a number of includes as follows; public abstract class DataContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>, IDataContext { public DataContext(DbContextOptions options) : base(options) { } // reduced for brevity public T Get<T>(int id, params Expression<Func<T, object>>[] includes) where T : class, IEntity { return this.Set<T>().Include(includes).FirstOrDefault(x => x.Id == id); } I

EF models. Navigation properties can only participate in a single relationship

随声附和 提交于 2020-01-24 16:00:11
问题 I have my entities like this, they are closely linked. public class Game { public int Id { get; set; } public int FirstTeamId { get; set; } public Team FirstTeam { get; set; } public int SecondTeamId { get; set; } public Team SecondTeam { get; set; } public Stadium Stadium { get; set; } public DateTime Date { get; set; } public GameStatus Result { get; set; } public Game(DateTime date , GameStatus result ) { Date = date; Result = result; } } public class Player { public int Id { get; set; }

EF models. Navigation properties can only participate in a single relationship

放肆的年华 提交于 2020-01-24 16:00:06
问题 I have my entities like this, they are closely linked. public class Game { public int Id { get; set; } public int FirstTeamId { get; set; } public Team FirstTeam { get; set; } public int SecondTeamId { get; set; } public Team SecondTeam { get; set; } public Stadium Stadium { get; set; } public DateTime Date { get; set; } public GameStatus Result { get; set; } public Game(DateTime date , GameStatus result ) { Date = date; Result = result; } } public class Player { public int Id { get; set; }