entity-framework-core

EF Core `update-database` on MySql fails with `__EFMigrationsHistory' doesn't exist`

别来无恙 提交于 2019-12-01 11:18:23
I Create new project in .net core 1.1 with individual user account identity. I Add MySql Provider in Startup.cs: services.AddDbContext<ApplicationDbContext>(options => options.UseMySQL(Configuration.GetConnectionString("DefaultConnection"))); But when i try do update-database i get error like this: MySql.Data.MySqlClient.MySqlException: Table 'cloud.__EFMigrationsHistory' doesn't exist at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean

Change MigrationsHistoryTable column names in EF Core

混江龙づ霸主 提交于 2019-12-01 11:00:48
I have a standardized all table and column names in my EF Core database to use snake_case. I was able to change the migrations history table name and schema to match the rest of the database, but I am not able to find a way to change the columns from MigrationId to migration_id and ProductVersion to product_version . Any ideas on how this could be done? Here is an example of how to do it on SQL Server. First, create a custom implementation of SqlServerHistoryRepository overriding ConfigureTable . class MyHistoryRepository : SqlServerHistoryRepository { public MyHistoryRepository(

MVC Routing template to represent infinite self-referential hierarchical category structure

给你一囗甜甜゛ 提交于 2019-12-01 10:44:08
I have a product category table to represent a hierarchical category structure, a typical Parent-Child relationship table in the database. Fill it with Guitar Center's data as an example: If you render them to a page with <ul> and <li> : Texts in blue are the URLs I would like to generate. For any given category, the link consists of its slug and its parents' slugs. Note that the example I listed only has 2 parent-child levels. In theory, with the self-referential structure, any child could have infinite parents. Questions: How to set up routing template to achieve that? If the routing

Entity Framework Core / SQLite: GroupJoin after Join “flattens” results

女生的网名这么多〃 提交于 2019-12-01 10:32:41
Using Entity Framework Core 2.1 and an SQLite database, I get different behaviour from a LINQ GroupJoin if I use it after another Join. It is not clear if this is a bug or if there is something I am overlooking. I have created a minimal VS2017 project to reproduce this, which can be found here: https://gitlab.com/haddoncd/EntityFrameworkGroupJoinIssue In this example, I get one object for each row in the Blog table, each containing multiple PostTitles: db.Blogs .GroupJoin( db.Posts, s => s.BlogId, p => p.BlogId, (s, ps) => new { s.BlogId, s.BlogTitle, PostTitles = ps.Select(p => p.PostTitle),

Entity Framework 7 Identity Seed

柔情痞子 提交于 2019-12-01 10:30:29
We are using Code First with EF7 and I would like to add a column which has an Identity Seed starting at another value other to 1. Currently we can set it to auto increment via the EntityTypeBuilder during migrations using: entityBuilder.Property(e => e.PropertyName).ValueGeneratedOnAdd(); However I cannot find out how to change the identity seed. Does it still need to be updated like it was with other versions of EF? e.g. writing some custom sql and running this during migration? How to seed identity seed value in entity framework code first for several tables How do I set Identity seed on an

Can't add Entity Framework Core migrations to .NET Standard 2.0 project

☆樱花仙子☆ 提交于 2019-12-01 10:16:02
问题 I've got a Solution with many projects. One of them ( Domain ) is a .NET Standard 2.0 project where I made my EF Core DbContext implementation for which I want to enable database migrations . I saw various blogs and Q/A forums where the problem was explained but none of the proposed solutions seem to work for me because of the .NET Core newer version or (probably) for my particular solution configuration. Solution projects Engine (.NET Core 2.1 Console App) Web API (.NET Core 2.1 Library)

How to only select specific properties from an object graph in Entity Framework?

巧了我就是萌 提交于 2019-12-01 10:05:40
问题 I have a simple data model car - make - model - year - colour - engine - model - no. cylinders - size - etc - fuel tank - model - capacity - fuel type - etc - etc So I have 'car', 'engine' and 'fuel tank' entities. Each of which have many properties. I want a list of all the 100s of cars but only want to show the following selected properties: car.make, car.model, car.year, car.engine, car.size, car.fueltype . I can certainly use .include to bring back sub-entities in the object graph but

What is the equivalent of .Configuration.ProxyCreationEnabled in EF Core?

旧时模样 提交于 2019-12-01 09:48:37
问题 What is the equivalent of .Configuration in Entity Framework Core? Receiving error below Code Examples: List<DocumentStatus> documentStatuses; using (var db = new ModelDBContext()) { db.Configuration.ProxyCreationEnabled = false; documentStatuses = db.DocumentStatus.ToList(); } using (var db = new ModelDBContext()) { db.Configuration.ProxyCreationEnabled = false; //Expression<Func<Owner, bool>> predicate = query => true; db.Configuration.ProxyCreationEnabled Error Messages throughout: Error

EF Core column name from mapping api

北战南征 提交于 2019-12-01 09:29:46
In EF 6.1 the mapping API was introduced where we could finally get access to table names and column names. Getting the table name is a very nice change in EF Core, but I have not yet uncovered how to get the column names. For anyone interested here is how I got the table name in the latest version (RC1) context.Model.FindEntityType(typeof(T)).SqlServer().TableName What is the current method to get column names or is this not available yet? Jonathan Magnan var columnNames = ctx.Model.FindEntityType(typeof (T)) .GetProperties().Select(x => x.SqlServer().ColumnName) .ToList(); Also var

How to load the navigation property with EF Core?

你说的曾经没有我的故事 提交于 2019-12-01 09:11:00
In EF6 we had such option: context.Set<TEntity>().Attach(entity); context.Entry(entity).Collection("NavigationProperty").Load(); Since EF Core "goes 100% strictly typed" they have removed Collection function. But what should be used instead? Added: I mean how to load includes/"navigation collection properties" for ATTACHED entity? Explicit Loading was added in Entity Framework Core v1.1. See Microsoft Docs From the docs: using (var context = new BloggingContext()) { var blog = context.Blogs .Single(b => b.BlogId == 1); context.Entry(blog) .Collection(b => b.Posts) .Load(); context.Entry(blog)