entity-framework-core

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

给你一囗甜甜゛ 提交于 2021-02-08 02:09:03
问题 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

Get Db context inside data access layer

走远了吗. 提交于 2021-02-07 21:39:11
问题 I have some problems with EF-Core that I'm trying to figure out. I use the startup code in the MVC Core application to initalize the db context. This is my DB context: public class AccountsDBContext : DbContext { public AccountsDBContext(DbContextOptions<AccountsDBContext> options) :base(options) { } // ... } And startup code: public void ConfigureServices(IServiceCollection services) { // Inject the account db services.AddDbContext<AccountsDBContext>(options => options.UseMySQL(Configuration

Get Db context inside data access layer

杀马特。学长 韩版系。学妹 提交于 2021-02-07 21:38:55
问题 I have some problems with EF-Core that I'm trying to figure out. I use the startup code in the MVC Core application to initalize the db context. This is my DB context: public class AccountsDBContext : DbContext { public AccountsDBContext(DbContextOptions<AccountsDBContext> options) :base(options) { } // ... } And startup code: public void ConfigureServices(IServiceCollection services) { // Inject the account db services.AddDbContext<AccountsDBContext>(options => options.UseMySQL(Configuration

LINQ Many-To-Many Where

谁说胖子不能爱 提交于 2021-02-07 20:50:41
问题 How do I write an EF7 (Core) / SQL Friendly Many-To-Many LINQ Query? Say for example I have many Friends that speak Many Languages, and I want to find all friends for a given set of languages. class Friend { Guid Id { get; set; } ICollection<FriendLanguage> Languages { get; set; } } class Language { Guid { get; set; } ICollection<FriendLanguage> Friends { get; set; } } class FriendLanguage { Friend { get; set; } Language { get; set; } } Given I have a set of language IDs IEnumerable<Guid> , I

Map navigation property to main table

北城余情 提交于 2021-02-07 20:43:14
问题 I have class Contract with two properties TotalAmount and InstallmentAmount public class Contract { public int ContractId { get; set; } public Amount TotalAmount { get; set; } public Amount InstallmentAmount { get; set; } //other Amounts } public class Amount { public decimal Value { get; set; } public string Currency { get; set; } } Is it possible to configure Entity Framework so it can create one table Contract with structure like below: -----------------------------------------------------

Map navigation property to main table

点点圈 提交于 2021-02-07 20:41:45
问题 I have class Contract with two properties TotalAmount and InstallmentAmount public class Contract { public int ContractId { get; set; } public Amount TotalAmount { get; set; } public Amount InstallmentAmount { get; set; } //other Amounts } public class Amount { public decimal Value { get; set; } public string Currency { get; set; } } Is it possible to configure Entity Framework so it can create one table Contract with structure like below: -----------------------------------------------------

How to call Stored Procedure with join on multiple tables in Entity Framework Core?

喜欢而已 提交于 2021-02-07 19:16:11
问题 I have to call a stored procedure which is selecting records from multiple tables. I have tried the following code, but it's returning null for columns from other tables than the entity class. private async Task<IEnumerable<TEntity>> InvokeStoredProcedureAsync(string input = "") { var storedProcedureName = "sp_BulkSelect"; using (var db = new MyDbContext(_options)) { var result = await db.Set<TEntity>().FromSql(storedProcedureName + " @inputIds", new SqlParameter("inputIds", input))

How to call Stored Procedure with join on multiple tables in Entity Framework Core?

隐身守侯 提交于 2021-02-07 19:15:15
问题 I have to call a stored procedure which is selecting records from multiple tables. I have tried the following code, but it's returning null for columns from other tables than the entity class. private async Task<IEnumerable<TEntity>> InvokeStoredProcedureAsync(string input = "") { var storedProcedureName = "sp_BulkSelect"; using (var db = new MyDbContext(_options)) { var result = await db.Set<TEntity>().FromSql(storedProcedureName + " @inputIds", new SqlParameter("inputIds", input))

Is it possible to load nested navigation properties using EntityEntry.Reference?

走远了吗. 提交于 2021-02-07 18:25:42
问题 Take these example classes: class TemplatePart { public PartStock stock {get; set;} ...other POCOs } class PartStock { public Part part {get; set;} ...other POCOs } class Part { public PartName name {get; set;} ...other POCOs } Now, suppose I already have an entity for a TemplatePart . I can do this: var entry = context.Entry(templatePart); entry.Reference(x => x.PartStock).Load(); That would load the navigation property for the PartStock . But how do I do this: entry.Reference(x => x

Unexpected behaviour with Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ForEachAsync<T>()

送分小仙女□ 提交于 2021-02-07 14:49:27
问题 Here are the steps to reproduce. The below program copies 10,000 rows from one SQL table to another using .Net Core console app and EF Core. The program inserts records in 100 batches, and (this is important!) it creates a new instance of DbContext for each insert. 1) Create SQL Server database, and the "Froms" and "Tos" tables: create table Froms ( Id int identity(1, 1) not null, Guid [uniqueidentifier] not null, constraint [PK_Froms] primary key clustered (Id asc) ) go create table Tos ( Id