entity-framework-core

How to Specify Entity Framework Core Table Mapping?

旧时模样 提交于 2019-12-05 14:07:21
问题 I've made a simple Entity Framework ASP Core Application which works but I do not know why: I've made a context like this: public class AstootContext : DbContext { public AstootContext(DbContextOptions<AstootContext> options) : base(options) { } public DbSet<Account> Accounts { get; set; } public DbSet<User> Users { get; set; } } And I have two tables with models like this: public class Account { public int Id { get; set; } public string Username { get; set; } public string PasswordHash { get

How can I stop EF 7 mapping an entity property to a column?

允我心安 提交于 2019-12-05 13:43:00
问题 In EF 6, I can add a NotMapped attribute to the property, then it will not be mapped to a column. How can I do this in EF 7? 回答1: We haven't implemented data annotations yet. (See #107) You should be able to do it using the Fluent API. modelBuilder.Entity<MyEntity>().Ignore(e => e.NotMappedProperty); 回答2: Just to add on to Ricky and bricelam's answer, There are two ways to ignore a property: Data annotations on model public class Blog { public int BlogId { get; set; } public string Url { get;

Entity Framework Core Auto Generated guid

可紊 提交于 2019-12-05 13:40:42
Can some One guide me I want primeryKey of a table as guid having db generated value on insert. [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } but it's giving error The seed entity for entity type 'User' cannot be added because there was no value provided for the required property 'Id'. Here is my actual model classes and DbContxt class: public class BaseModel { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public DateTime CreatedOn { get; set; }

How to avoid n+1 queries in EF Core 2.1?

爱⌒轻易说出口 提交于 2019-12-05 13:11:56
I'm using EF Core 2.1 preview which was supposed to reduce N+1 queries problem. I'm trying to make query, that selects Forum Threads with authors of posts: dbContext.ForumThreads .Include(t => t.Posts) .Take(n) .Select(t => new { t.Id, t.Title, PostAuhtors = t.Posts.Select(p => p.Author).Take(5) }).ToArray(); This produces n+1 queries: For each ForumThread it selects post authors The schema is simple: public class ForumThread { public Guid Id {get;set;} public string Title {get;set;} public ICollection<ForumPost> Posts {get;set;} } public class ForumPost { public Guid Id {get;set;} public

Apply all IEntityTypeConfiguration derived classes in EF Core

懵懂的女人 提交于 2019-12-05 12:54:59
Does anyone know of a way or have an implementation to apply ALL classes that derive from IEntityTypeConfiguration<> to the DbContext at runtime? There doesn't seem to be anything built in and loading each one manually via: protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.ApplyConfiguration(new Table1Config()) modelBuilder.ApplyConfiguration(new Table2Config()) ... modelBuilder.ApplyConfiguration(new TableNConfig()) } is going to prove rather tedious for a database with many tables. For EF Core <= 2.1 You can write an extension method as follows: public static

StartsWith() doesn't translate to Like('abc%') in LINQ

感情迁移 提交于 2019-12-05 12:39:21
I have the following asp.net core LINQ code: List<UserSearchResult> results = await db.ApplicationUsers.Where(u => u.Name.StartsWith(name) && !u.Deleted && u.AppearInSearch) .OrderByDescending(u => u.Verified) .ThenBy(u => u.DateAdded) // Added to prevent duplication of results in different pages .Skip(page * recordsInPage) .Take(recordsInPage) .Select(u => new UserSearchResult() { Name = u.Name, Verified = u.Verified, PhotoURL = u.PhotoURL, UserID = u.Id, Subdomain = u.Subdomain }).ToListAsync(); Unfortunately this translates to the following: SELECT [t].[Name], [t].[Verified], [t].[PhotoURL]

Linq select distinct count performed in memory

こ雲淡風輕ζ 提交于 2019-12-05 12:30:34
I'm working on understanding how LINQ converts to SQL. I have the following query I'm trying to generate using LINQ. SELECT [OrganizationId] ,[DepartmentId] ,[LocationName] ,[Group1] ,[Group2] ,[Group3] ,[BooklistId] ,[BooklistName] ,COUNT(DISTINCT [OrdererId]) ,MAX([ExpectedDate]) FROM [Orders] WHERE ([DepartmentId] IS NOT NULL AND ([DepartmentId] = '<Guid>')) AND ([Schoolyear] = '2018') GROUP BY [OrganizationId] ,[DepartmentId] ,[LocationName] ,[Group1] ,[Group2] ,[Group3] ,[BooklistId] ,[BooklistName] ORDER BY [BooklistName] With indexes this query performs under 200ms. My LINQ query is the

Cannot run migration on Entity Framework 7 beta4 in class library package

隐身守侯 提交于 2019-12-05 11:52:52
I am unable to execute migration command in a class library package. My dnvm version is (from global.json): "sdk": { "version": "1.0.0-beta5-12021" } I'm importing this in my project JSON. "dependencies": { "EntityFramework.Commands": "7.0.0-beta4", "EntityFramework.SqlServer": "7.0.0-beta4", "Microsoft.CSharp": "4.0.0-beta-22816", "System.Collections": "4.0.10-beta-22816", "System.Linq": "4.0.0-beta-22816", "System.Threading": "4.0.10-beta-22816", "EntityFramework.Core": "7.0.0-beta4" }, "commands": { "ef": "EntityFramework.Commands" } My OnConfiguring method in my DbContext inherited class

EF Core Eager Loading nested collections

走远了吗. 提交于 2019-12-05 10:47:18
I'm trying to load a related modal in Entity Framework Core but for some reason there's a nested collection being loaded when I haven't asked for it in my Include() call. Here's my two models - Driver.cs public partial class Driver : IBaseEntity { public short DriverId { get; set; } public string Surname { get; set; } public string Initials { get; set; } public byte DriverStatusTypeId { get; set; } public DriverStatusType DriverStatusType { get; set; } } DriverStatusType.cs public partial class DriverStatusType { public DriverStatusType() { Drivers = new HashSet<Driver>(); } public byte

How does EF Core Modified Entity State behave?

青春壹個敷衍的年華 提交于 2019-12-05 10:36:03
Does it matter we put the entity state = modified after changes or before making changes? using (var db = new LakshyaContext()) { foreach (var category in db.Categories) { db.Entry(category).State = EntityState.Modified; // before category.Count = 25; //Making Changes db.Entry(category).State = EntityState.Modified; //After } db.SaveChanges(); } So first, let's get the most important thing out of the way: You are right. In your example, you don't need to manually call db.Entry(category).State = EntityState.Modified . This is because you are loading the entries (categories) from the context