entity-framework-core

Create a non-clustered index in Entity Framework Core

空扰寡人 提交于 2019-12-07 03:27:03
问题 Using Entity Framework Core, I want to have a Guid PK, without suffering page fragmentation in the database. I have seen this post and this. Although it was possible in EF6, the way it's done seems to have changed. Is it possible to create a non-clustered primary key in Entity Framework Core and have an additional index? Q&A Answer below. 回答1: It is possible using EntityFrameworkCore v1.0.1 or greater. The following code gets the desired result: using System; using Microsoft

Package 'Microsoft.EntityFrameworkCore.SqlServer' is incompatible with 'all' frameworks in the project

此生再无相见时 提交于 2019-12-07 02:53:31
问题 I was trying to add the package named Microsoft.EntityFrameworkCore.SqlServer by running the command. dotnet add package Microsoft.EntityFrameworkCore.SqlServer in Visual Studio Code, but I get this error: Package Microsoft.EntityFrameworkCore.SqlServer 2.0.0 is not compatible with netcoreapp1.1 (.NETCoreApp,Version=v1.1). Package Microsoft.EntityFrameworkCore.SqlServer 2.0.0 supports: netstandard2.0" (.NETStandard,Version=v2.0). Package 'Microsoft.EntityFrameworkCore.SqlServer' is

Bulk-Insert to MySQL in Entity-Framework Core

老子叫甜甜 提交于 2019-12-07 02:46:46
问题 I have a list consisting of ~10,000 objects (let's say of class Person ) that I need to insert to a MySQL table. If I use the regular DbContext.SaveChanges() , it takes 60-70 seconds to issue, which I need to reduce drastically. I've found several extensions for bulk-insertions: EF extensions (not free, so no option) BulkExtensions (no MySQL, only SQL Server) EFBulkInsert (no MySQL,only SQL Server) ... Unfortunately, non seem to exist for MySQL databases. Does anybody know of one for MySQL?

The instance of entity type 'Item' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked

◇◆丶佛笑我妖孽 提交于 2019-12-07 02:06:02
问题 I am aware that such question has already been asked, but solution did not help me. [Fact] public async Task UpdateAsync() { string newTitle = "newTitle1"; int newBrandId = 3; var item = await storeContext.Items.AsNoTracking().FirstOrDefaultAsync(); item.BrandId = newBrandId; item.Title = newTitle; storeContext.Entry(item).State = EntityState.Detached; await service.UpdateAsync(item); // exception inside var updatedItem = await storeContext.Items.AsNoTracking().FirstOrDefaultAsync(); Assert

Entity core Mapping entity types with join table

强颜欢笑 提交于 2019-12-06 23:07:25
I've started using entity core and I ran into one problem. I'm using automapper in my project and don't know how to populate my join table(BookAuthor). In MVC I worked with ICollections but entity core does not yet support working with many to many relationship entities and now I have to work with join table. Question: How can i map my Bookviewmodel to the Book? I don't know what should I do with my BookAuthors list. Maybe I shouldn't populate it? I tried something like that but it's not working. CreateMap<BookViewModel, Book>() .ForMember(b => b.BookAuthors, opt => opt.MapFrom(b => b.Authors

Has EF6+ / 7 added any ways that I can add update child tables?

元气小坏坏 提交于 2019-12-06 20:29:29
I have two tables: public AdminTest() { this.AdminTestQuestions = new List<AdminTestQuestion>(); } public int AdminTestId { get; set; } public string Title { get; set; } public virtual ICollection<AdminTestQuestion> AdminTestQuestions { get; set; } } public partial class AdminTestQuestion { public int AdminTestQuestionId { get; set; } public int AdminTestId { get; set; } public System.Guid QuestionUId { get; set; } public virtual AdminTest AdminTest { get; set; } } I am using the following EF6 code to add a new adminTest (with its adminTestQuestions) to the database: public async Task

.NET Core database model changes in production

不想你离开。 提交于 2019-12-06 19:42:28
I follow code first approach to generate my databases by EFCore. Now during development one of my models changed. How can I update the corresponding database tables on a live system at one of our customers? I do not want to dump or delete any data of course. I'm still on .NET Core 1.1.0. I fear you'll have to swallow the pill then. Database.EnsureCreated() is only useful for developing, where you don't have to preserve data. If you have to preserve data or change the table schema, you have to use migrations or database scaffolding (create models from Database schema). In any case, do a backup

Why is Visual Studio telling me I need to reference System.Private.CoreLib?

烈酒焚心 提交于 2019-12-06 19:19:51
问题 I'm trying out EF Core for the first time and have coded a very simple MVC app to get my feet wet. I am using a method for seeding the database found in the UnicornStore project where they write some code in Startup.cs to migrate the database and then run a seed method. Before they call the seed method, they run this DbContext extension method to check if all migrations have been applied: using System; using System.Linq; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore

How to scaffold DbContext with plural DbSet property names in Entity Framework Core?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 17:43:31
问题 I use Scaffold-DbContext command in Package Manager Console to create and re-create context and entities for an existed SQL Server database: Scaffold-DbContext -provider EntityFramework.MicrosoftSqlServer -connection "my connection string" It works perfectly except one thing: DbSet 's have property names in singular form: public partial class MyDbContext : DbContext { public virtual DbSet<Request> Request { get; set; } public virtual DbSet<RequestHeader> RequestHeader { get; set; } } I prefer

Entity Framework core .Include() issue

随声附和 提交于 2019-12-06 17:25:26
问题 Been having a play about with ef core and been having an issue with the include statement. For this code I get 2 companies which is what i expected. public IEnumerable<Company> GetAllCompanies(HsDbContext db) { var c = db.Company; return c; } This returns [ { "id":1, "companyName":"new", "admins":null, "employees":null, "courses":null }, { "id":2, "companyName":"Test Company", "admins":null, "employees":null, "courses":null } ] As you can see there are 2 companies and all related properties