entity-framework-core

How to include related tables in DbSet.Find()?

陌路散爱 提交于 2019-12-01 08:10:55
If I want to include related objects in an EF7 query, it's nice and easy: var myThing = db.MyThings .Include(t => t.RelatedThing) .Where(t => t.SomeCondition == true) .ToList(); Also, there's a nice method on the DbSet<T> that makes it easy to load a single object by its key: var myThing = db.MyThings.Find(thingId); But now I want to load myThing by its Id, along with its RelatedThing . Unfortunately (and understandably) .Find() is a method of DbSet<T> , not IQueryable<T> . Obviously I could do this: var myThing = db.MyThings .Include(t => t.RelatedThing) .SingleOrDefault(t => t.MyThingId ==

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

耗尽温柔 提交于 2019-12-01 08:07:40
问题 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&

Scaffold-DbContext throws error “Could not find assembly” in .net core

空扰寡人 提交于 2019-12-01 07:52:54
问题 I am using .net core and entity framework core 1.1.0. while trying the following command in Package Manager Console Scaffold-DbContext "Server=MyServer\\MyInstance;Database=MyDB;user=MyUsername;password=MyDbPassword;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -t Table1,Table2 I am getting this error Could not find assembly 'D:\Work\Projects\src\MyProject\src\MyProject.Api.\bin\Debug\net461\win7-x64\MyProject.Data.exe'. MyProject.Data is a net core library. MyProject.Api is a

Entity Framework 7 Identity Seed

眉间皱痕 提交于 2019-12-01 07:44:22
问题 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

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

*爱你&永不变心* 提交于 2019-12-01 07:32:38
问题 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

How should I define a composite primary key for an existing database table?

可紊 提交于 2019-12-01 06:50:41
问题 I'm working on a Core MVC project that reads data (no writing required) from a pre-existing database. Unfortunately this database is a total mess, but I cannot change anything in it (and even if I could, I wouldn't touch it with a 10-foot pole). Some of the relevant problems of the database are the following: The tables don't have any foreign key relations to each other, and contain data that would have best been entered into a sub-table; whoever created the database seems to have used tables

how to apply common configuration to all entities in ef core

我只是一个虾纸丫 提交于 2019-12-01 06:34:21
I have entities derived from a base entity in my application which uses ef core code-first approach. Base class public abstract class BaseEntity<T> : IEntity<T> { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public T Id { get; set; } object IEntity.Id { get { return Id; } set { } } private DateTime? createdOn; [DataType(DataType.DateTime)] public DateTime CreatedOn { get => createdOn ?? DateTime.Now; set => createdOn = value; } [DataType(DataType.DateTime)] public DateTime? ModifiedOn { get; set; } public bool IsDeleted { get; set; } // Auto increment for all entities. public

Entity Framework Core 1.0 CurrentValues.SetValues() does not exist

ⅰ亾dé卋堺 提交于 2019-12-01 06:18:52
问题 I'm attempting to update an entity and its related child entities using Entity Framework Core 1.0 RC 1, where the entities are detached from DbContext. I've done this previously using a solution similar to the one described in this answer. However, it seems that we are no longer able to do the following using Entity Framework 7: DbContext.Entry(existingPhoneNumber).CurrentValues.SetValues(); Visual Studio complains that: EntityEntry does not contain a definition for 'CurrentValues' etc... I

How to load the navigation property with EF Core?

↘锁芯ラ 提交于 2019-12-01 06:16:16
问题 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? 回答1: Explicit Loading was added in Entity Framework Core v1.1. See Microsoft Docs From the docs: using (var context = new BloggingContext()) { var blog = context

Update Database after Model Changes - Entity Framework 7

好久不见. 提交于 2019-12-01 05:55:47
I have created an app using the lastest ASP.NET5 MVC 6 Entity Framework 7 and setup migrations using dnx . ef migration add Initial dnx . ef migration apply This works but when I make a change to the model the database is not updated. I would like to have the database automatically update after a model change when I run the program. My research only points me to old information that doesn't seem to be appropriate to Entity Framework 7. My current code: public ApplicationDbContext(): base() { if (!_created) { Database.AsRelational().ApplyMigrations(); _created = true; } } Can someone point me