entity-framework-core

A DbContext instance cannot be used inside OnConfiguring since it is still being configured at this point

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 06:17:52
I'm trying to build a custom view location system. public class myViewLocationExpander : IViewLocationExpander { private myDBContext _context; public myViewLocationExpander (myDBContext context) { _context = context; } public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) { _context.Property.//..... //Error happened here Some other codes } And in my startup file public void ConfigureServices(IServiceCollection services) { //other codes services.AddMvc(); services.Configure<RazorViewEngineOptions>(options => { options

AsNoTracking on context properties, query or ChangeTracker?

久未见 提交于 2019-12-05 05:51:27
I noticed that it is possible to disable tracking in three different ways: via AsNoTracking on context properties via AsNoTracking on the final query before executing it via context.ChangeTracker.QueryTrackingBehavior Is there any difference between these three approaches if I want to disable tracking for everything? If I previously used AsNoTracking after each context property and now I replace it with only a single call on the final query (or disable it via the ChangeTracker ) will it have the same effect? AsNoTracking and AsTracking are extension methods of IQueryable<T> , thus are

DbSet.Include operator for ef7 accepting string path

主宰稳场 提交于 2019-12-05 04:42:16
EF6 has an overload of DbSet.Include which accepts a string parameter representing a dot-separated list of related objects to return in the query results. It is useful for eager-loading entities in a multi-level object graph. For example: var order = await _dbContext.Orders .Include(o => o.Customer) .Include("OrderDetails.Product") // dot-delimited path .SingleOrDefaultAsync(o => o.OrderId == id); This will return both related order details and populate the Product property of each detail by generating a SQL statement that joins OrderDetail and Product tables. I am looking a way to do this

The specified deps.json '$$$' does not exist

こ雲淡風輕ζ 提交于 2019-12-05 04:32:56
I am rather new to the .NET Core, and I got a .NET Core WebAPI project MyWebApp, also, i have .Net Core Class Library project MyLib using EntityFrameworkCore When i try to use Add-Migration, i get the error The specified deps.json [...\MyWebApp\bin\Debug\netcoreapp1.1\MyWebApp.deps.json] does not exist Inspecting the folder, I noticed I have this a file in [...\MyWebApp\bin\Debug\netcoreapp1.1\win10-x64\MyWebApp.deps.json] but i really can't figure what i am supposed to do to resolve this. myWebApi project.json: { "dependencies": { "ShopManager": "1.0.0-*", "Microsoft.AspNetCore.StaticFiles":

many to many EF7

时光怂恿深爱的人放手 提交于 2019-12-05 03:34:07
Models: public partial class Film { public int FilmID { get; set; } public virtual ICollection<Genre> Genres { get; set; } } public class Genre { public int GenreID { get; set; } public virtual ICollection<Film> Films { get; set; } } OnModelCreating using EF6 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Film>() .HasMany(e => e.Genres) .WithMany(e => e.Films) .Map(m => m.ToTable("Genre_Film").MapLeftKey("Films_IdFilm").MapRightKey("Genres_IdGenre")); } I use SQLite. How I can do the same using EF7? The docs for EF7 says how this can be achived: http

Entity Framework Core: private or protected navigation properties

血红的双手。 提交于 2019-12-05 02:54:10
Is it somehow possible to define navigation properties in EFCore with private or protected access level to make this kind of code work: class Model { public int Id { get; set; } virtual protected ICollection<ChildModel> childs { get; set; } } You have two options, using type/string inside the model builder. modelBuilder.Entity<Model>(c => c.HasMany(typeof(Model), "childs") .WithOne("parent") .HasForeignKey("elementID"); ); Not 100% sure it works with private properties, but it should. Update: Refactoring-safe version modelBuilder.Entity<Model>(c => c.HasMany(typeof(Model), nameof(Model.childs)

Entity Framework 7 Reverse Engineering ASP.NET 5

你。 提交于 2019-12-05 02:37:10
问题 I have been trying to migrate my database into web app model using EF7 _4. Couple things I realized is that the syntax/approach how to configure particular entities has been changed. Normally you generate POCO classes by using EF Power Tools (EF6), which generates entity types and entity map as configuration. All clean, all works. My question is, does someone experience how to do it with EF7? From the msdn blog I have found here about Entity Framework 7 Beta 4 the features for reverse

IHostingEnvironment.WebRootPath is null when using EF7 commands

大城市里の小女人 提交于 2019-12-05 02:16:07
Problem When I try to add a migration to my code, e.g: dnx ef migrations add initial , the env.WebRootPath in Startup(IHostingEnvironment env) is null. This will give me compilation errors when adding a new migration or updating the database. The Code In the Startup.cs class I have these lines in the constructor: public Startup(IHostingEnvironment env) { // ... MyStaticClass.Initialize(env.WebRootPath); // ... _hostingEnvironment = env; } Here env.WebRootPath is null and in the Initialize function this throws an exception. In the ConfigureServices function of the Startup.cs I resolve my class

EF Core implementing Table-Per-Concrete-Type with fluent mapping of abstract base class

只谈情不闲聊 提交于 2019-12-05 01:01:57
问题 Suppose that you have two entities derived from an abstract base class and you want implement Table-Per-Concrete-Type. Entities like below: public abstract class EntityBase { public int Id { get; set; } public string CreatedBy { get; set; } public DateTime CreatedAt { get; set; } } public class Person : EntityBase { public string Name { get; set; } } public class PersonStatus : EntityBase { public string Title { get; set; } } And you don’t want to use attribute in the abstract base class

Entity Framework 7 DbContext scaffold

泄露秘密 提交于 2019-12-05 00:35:46
I am trying to generate a DbContext for an existing database structure using ASP.NET 5 and Entity Framework 7. Not surprisingly, there isn't a lot of documentation surrounding how to do this easily. Additionally, I want to scaffold ONLY the context; there are ~900 tables and I only care about a few of them, I don't need a model class for each one. I've been using the commands specified here and here with little luck. So, I guess I have two questions: Where are the generated context files located? I run the command in the command prompt with no failure, but nothing else happens. I know I'm at