entity-framework-core

Table per Type in Entity Framework Core 2.0

筅森魡賤 提交于 2020-01-23 03:08:03
问题 These are my models: public class Company { public int CompanyId { get; set; } public string Name { get; set; } public string Address { get; set; } public string Email { get; set; } public string Url { get; set; } //... } public class HeadOffice : Company { public int HeadOfficeId { get; set; } public virtual List<BranchOffice> BranchOffice { get; set; } = new List<BranchOffice>(); } public class BranchOffice : Company { public int BranchOfficeId { get; set; } public virtual HeadOffice

Cannot access a disposed object for DbContext in .NET Core for Async method

别说谁变了你拦得住时间么 提交于 2020-01-23 02:59:12
问题 I am having a weird problem in one of my microservice web api. My async GET methods throw a Cannot access a disposed object exception for my DbContext except for the very first time they are invoked. I tried looking online for an answer but nothing worked. I made sure my methods are not async void and I await the necessary calls. Since my POST and DELETE methods work fine, I am fairly certain that the real culprit is the IMapper instance. I think it might always point to the first instance of

How to make a join table using EF core code first

≡放荡痞女 提交于 2020-01-22 15:34:45
问题 I have these three models: public class Card { public int ID { get; set; } public string Name { get; set; } public string Class { get; set; } public string Image { get; set; } } public class Deck { public int ID {get; set;} public string Name {get; set;} public string Class {get; set;} public virtual ICollection<DeckCard> DeckCards {get; set;} } public class DeckCard { public int ID {get; set;} public int DeckID {get; set;} public int CardID {get; set;} } I want to use the DeckCard model as a

One-to-One relationships in Entity Framework 7 Code First

▼魔方 西西 提交于 2020-01-22 12:03:28
问题 How to configure One-to-One or ZeroOrOne-to-One relationships in Entity Framework 7 Code First using Data Annotations or Fluent Api? 回答1: You can define OneToOne relationship using Fluent API in Entity Framework 7 as below class MyContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<BlogImage> BlogImages { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>() .HasOne(p => p.BlogImage) .WithOne(i => i.Blog)

One-to-One relationships in Entity Framework 7 Code First

狂风中的少年 提交于 2020-01-22 12:02:44
问题 How to configure One-to-One or ZeroOrOne-to-One relationships in Entity Framework 7 Code First using Data Annotations or Fluent Api? 回答1: You can define OneToOne relationship using Fluent API in Entity Framework 7 as below class MyContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<BlogImage> BlogImages { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>() .HasOne(p => p.BlogImage) .WithOne(i => i.Blog)

One-to-One relationships in Entity Framework 7 Code First

旧巷老猫 提交于 2020-01-22 12:02:28
问题 How to configure One-to-One or ZeroOrOne-to-One relationships in Entity Framework 7 Code First using Data Annotations or Fluent Api? 回答1: You can define OneToOne relationship using Fluent API in Entity Framework 7 as below class MyContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<BlogImage> BlogImages { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>() .HasOne(p => p.BlogImage) .WithOne(i => i.Blog)

Aggregate of aggregate with EF Core Linq2Sql

雨燕双飞 提交于 2020-01-21 19:18:28
问题 I have a ASP.NET Core 2.2 project with EF Core 2.2 Code-First DB. I have the following entities: Building, which is basically an address with some other important data. Floor, which contains the number of the floor. A building can have multiple floors. A floor has to have exactly one building where it's located. Room, which has a number. A floor can have multiple rooms. A room has to have exactly one floor. WorkGroup, which contains how many employees are int the group, whether the group is

EnitityFramework is very slow to compare strings because create a nvarchar sqlparameter instead of varchar

99封情书 提交于 2020-01-21 17:48:31
问题 I have this sample query: context.BarcodeTipiDoc.AsQueryable().Where(d => d.Barcode.CompareTo(minBarcode) > 0); That query runs very slow because Entity Framework creates SqlParameter for "minBarcode" as nvarchar instead of varchar . I tried to set column mapping: [Column("Barcode", TypeName = "varchar(21)")] public string Barcode { get; set; } but nothing changed. There is a way to tell to Entity Framework the right type of the sqlparameter? This query is almost instantaneous: DECLARE @_

Entity Framework 7 IDENTITY_INSERT

荒凉一梦 提交于 2020-01-21 13:25:39
问题 I need to store a large amount of user data in a database where every user already has an ID as a key. However, these users all out of a larger pool of users, therefore, the ID does not increment and cannot be automatically generated. I need to be able to manually set the ID. Is there an easy way to do this? 回答1: I've made some tests and this seems to work for me on EF 7.0.0 rc1-final: modelBuilder.Entity<Foo>().HasKey(x => x.Id); modelBuilder.Entity<Foo>().Property(x => x.Id)

Entity Framework Core: Include many-to-many related objects in WebAPI

試著忘記壹切 提交于 2020-01-21 09:07:07
问题 I'm not too familiar with the .NET framework but decided to try out ASP.NET Core and EF Core. I want to make a pretty simple Web API backend but I'm having trouble working with many-to-many relationships. I understand that I need to make a relationship table for the two entities, as in the example from this post: How to create a many to many relationship with latest nightly builds of EF Core? I also have my model builder creating the relationship as described here http://ef.readthedocs.io/en