entity-framework-core

Entity Framework Core: TPT

帅比萌擦擦* 提交于 2021-02-08 11:26:33
问题 .NET 5 preview 8 is just released and I've been testing the brand new TPT feature. I've installed the .NET 5 preview 8 Runtime and SDK. I created a .NET Core class library, installed the dotnet-ef preview 8 NuGet, updated the dotnet-ef tool with the following command (administrator): dotnet tool update --global dotnet-ef --version 5.0.0-preview.8.20407.4 I've build the following database model: public class MintPlayerContext : DbContext { // dotnet ef migrations add AddInitialEntities //

Entity Framework Core: TPT

巧了我就是萌 提交于 2021-02-08 11:26:30
问题 .NET 5 preview 8 is just released and I've been testing the brand new TPT feature. I've installed the .NET 5 preview 8 Runtime and SDK. I created a .NET Core class library, installed the dotnet-ef preview 8 NuGet, updated the dotnet-ef tool with the following command (administrator): dotnet tool update --global dotnet-ef --version 5.0.0-preview.8.20407.4 I've build the following database model: public class MintPlayerContext : DbContext { // dotnet ef migrations add AddInitialEntities //

EF Core - multiple counts in one sql request

对着背影说爱祢 提交于 2021-02-08 08:23:17
问题 I am trying to get multiple count stats from a table but I'm not getting what I want. Code var result = _db.Users.Select(g => new { count = _db.Users.Count(), acCount = _db.Users.Count(u => u.User.State == AccountState.AwaitingConfirmation) }); Sql request SELECT ( SELECT COUNT(*) FROM `users` AS `c` ) AS `count`, ( SELECT COUNT(*) FROM `users` AS `u` INNER JOIN `users` AS `u.User` ON `u`.`UserId` = `u.User`.`Id` WHERE `u.User`.`State` = 4 ) AS `acCount` FROM `users` AS `g` Expected Result

EF Core - multiple counts in one sql request

烂漫一生 提交于 2021-02-08 08:22:24
问题 I am trying to get multiple count stats from a table but I'm not getting what I want. Code var result = _db.Users.Select(g => new { count = _db.Users.Count(), acCount = _db.Users.Count(u => u.User.State == AccountState.AwaitingConfirmation) }); Sql request SELECT ( SELECT COUNT(*) FROM `users` AS `c` ) AS `count`, ( SELECT COUNT(*) FROM `users` AS `u` INNER JOIN `users` AS `u.User` ON `u`.`UserId` = `u.User`.`Id` WHERE `u.User`.`State` = 4 ) AS `acCount` FROM `users` AS `g` Expected Result

EF Core - Disposable DbContext and Attach() - or - DbContext as member - or - Disconnected Entities

时光总嘲笑我的痴心妄想 提交于 2021-02-08 06:43:28
问题 I'm not sure about how to correctly use the DbContext for Entities bound to a WPF DataGrid? How do I correctly "re-attach" and save changes to the database for all the entities that were loaded to the datagrid during UserControl load? I was using a DbContext as a member variable and ObservableCollection as DataSource for Datagrids. So everything was fine so far, no need to search for errors in the code below. Just to show what I have done so far. // Old code - working perfectly as desired

EF Core 2.2 - Two foreign keys to same table

不打扰是莪最后的温柔 提交于 2021-02-08 05:42:18
问题 I have a similar problem to the one posted here: Entity Framework Code First - two Foreign Keys from same table, however it's very old and doesn't apply to Core and I can't get the suggestions to work for me. Basically, I'm trying to create a fixture table which will have two foreign keys to the team table. A fixture is made up of a home team and an away team. Having nullable fields isn't an option. Consider a fixture, with two teams. public class Fixture { public int Id { get; set; } public

Entity Framework Core - issue with loading related entities that also contain related entities

半腔热情 提交于 2021-02-08 05:41:19
问题 I am using Entity Framework Core following Chris Sakell's blog here. He uses generics to manage his repositories and also a base repository that he uses for all the other repositories. Part of the base repository has the the following code for the retrieval of a single entity that also downloads related entities using the includeProperties option. Here is the generic code for a retrieving a single item. public T GetSingle(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>

Including SQLite DB file with data in the UWP application

冷暖自知 提交于 2021-02-08 05:23:11
问题 I'm trying to include a SQLite file with the UWP application. SQLite file has a number of tables in it populated with data. The database (for now, at least) is read-only and I'm planning to use EF Core to access the data. So i have two questions: What is the correct way to bundle this file with the application. How do I compose the file path to access that file from UWP applciation? Setting .sqlite file's "Copy to output directory" setting to "Copy always" seems to copy the file to bin folder

Connect multiple Databases to .NET core project via Entity Framework Core

梦想与她 提交于 2021-02-08 03:36:25
问题 I am trying to create a .NET core application that connects to multiple databases, but using one generic repository that contains the logic for all CRUD operations. What is the best way to achieve this? public Repository(ApplicationDbContext dbContext) { _dbContext = dbContext; _set = _dbContext.Set<T>(); } Above is the constructor of my repository. Here, I inject the ApplicationDbContext. I am looking for a way to make this ApplicationDbContext generic, so I only have to need only one

What is the best way to seed data with a lot of information using EntityFramework Core?

情到浓时终转凉″ 提交于 2021-02-08 02:14:44
问题 I have an entity configuration file and I seed data with the help of HasData , the example below. public class PublicationConfiguration : IEntityTypeConfiguration<Publication> { public void Configure(EntityTypeBuilder<Publication> builder) { builder.Property(p => p.Image) .HasMaxLength(256) .HasDefaultValue("default-publication.png") .IsRequired(); builder.Property(p => p.Title) .HasMaxLength(256) .IsRequired(); builder.Property(p => p.Value) .IsRequired(); builder.Property(p => p.PublisherId