entity-framework-core

Get all entities from DbSet<TEntity> with a property value that is in an IEnumerable

淺唱寂寞╮ 提交于 2019-12-13 04:01:48
问题 This seems like it would be really simple, but I cannot seem to resolve it. Using EF Core, I have a DbSet<Rule> Rules in my DbContext . public class Rule { public int Id { get; set; } public string Raw { get; set; } } I am trying to write a query where, given an IEnumerable<string> lines , give me all of the Rule s from the DbSet where its Raw value is an element in lines (exact match, not a substring of a value). For some time, I was using something like: private IQueryable<Rule>

How can I add to my list empty objects based on week number

﹥>﹥吖頭↗ 提交于 2019-12-13 03:56:23
问题 I'm getting data from db like this: Later I group them so it looks like Month 6 Week 2 Amount 228 And so on.. Here is the code: var yas = await _context.product .AsNoTracking() .Where(x => (x.PaymentDate != null && x.PaymentDate > DateTime.UtcNow.AddMonths(-4))).ToListAsync(); var grouped = yas.GroupBy(x => CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(x.PaymentDate ?? DateTime.UtcNow, CalendarWeekRule.FirstDay, DayOfWeek.Monday)) .Select(product => new productsDemoObject { Week =

How to implement left join on lambda linq expression with includes

北慕城南 提交于 2019-12-13 03:47:19
问题 I've a FilterAsync method to do the query with where clause and includes the navigation properties. Below is the method for the FilterAsync. public async Task<IQueryable<T>> FilterAsync<T>(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes) where T : class { return await Task.Run(() => { //Return first 250 records/objects from var query = _context.Set<T>().Where(predicate).AsQueryable().Take(250); return includes.Aggregate(query, (current, includeProperty) =>

EF Core: How to: Database generated string with specific rules?

流过昼夜 提交于 2019-12-13 03:47:13
问题 Is it possible with the fluent api to create a database generated string with specific rules? Like say: The string should start with "a" then a number that is incremented by 1 and minimum starting value is 10000. e.g. a10001, a10002,... 回答1: You could use a combination of HasSequence and HasDefaultValueSql Example works in SQL Server, not sure about other providers. public class Foo { public int FooId { get; set; } public string GeneratedString { get; set; } } public class FooContext :

How to create DbSet for ignored entity

心已入冬 提交于 2019-12-13 03:46:37
问题 I need to create DbSet for my ignored entity. Now I have in my modelBuilder next code: modelBuilder.Ignore<ExtendedDepartment>(); And inside my DbSet I have: public DbSet<ExtendedDepartment> ExtendedDepartments { get; set; } When I`m trying to get data from that Dbset I have error: InvalidOperationException: Cannot create a DbSet for ExtendedDepartment because this type is not included in the model for the context. So I am trying to ignore this table in migration, maybe I am doing smthing

Remove certain properties from object upon query EF Core 2.1

旧时模样 提交于 2019-12-13 03:39:52
问题 I'm trying to null or remove the ID entirely of all the queried IsoDataTables before returning them to frontend. The idea is that it should behave (in this case) as a template and I don't want it returning the id's back to me, nor do I want them to be removed in the frontend. var applicationType = await _context.ApplicationType .Include(m => m.IsoTableData) .AsNoTracking() .FirstOrDefaultAsync(m => m.Id == id); if (applicationType == null) { return NotFound(); } if (applicationType

ASP NET CORE Could not load System.Data.SqlClient

拈花ヽ惹草 提交于 2019-12-13 03:33:23
问题 I have an aspnetcore solution that I split into three projects: Project.Core dnxcore50 Main app - startup resides here and has Project.EF & Project.Model dependency Project.EF dotnet5.4 class libary - dbcontext resides here and has Project.Model dependency. dotnet-ef tools reside here Project.Models dotnet5.4 class library Using the dotnet cli tooling (dotnet-ef) I was able to add migrations to the project successfully. However when I execute update database to apply the migrations, I get an

Updating common field with user email

倾然丶 夕夏残阳落幕 提交于 2019-12-13 03:19:26
问题 I am trying to find out what the authorized user is currently trying to save the data and record their email. I have a basic setup in my dbcontext that does the update for timestamps but i cannot figure out how to access the user information: public override int SaveChanges() { AddTimestamps(); return base.SaveChanges(); } public override async Task<int> SaveChangesAsync() { AddTimestamps(); return await base.SaveChangesAsync(); } private void AddTimestamps() { var entities = ChangeTracker

How to write Inline If Statement(SQL IIF) in EFCore Select?

只愿长相守 提交于 2019-12-13 03:12:49
问题 I have the following Customer table: Id First Last LocationId 0 John Doe 2 1 Mary Smith 4 My use case requires column level permissions(predicated on a value in the Entity's table). How can I query like the following thru EFCore? SELECT Id, First, IIF(LocationId in(2), Last, '') FROM Customer; Whereby Last is returned only when LocationId == 2 . Can this be accomplished in Linq-to-Entities as a dynamic type? If not, can I use FromSql() and QueryTypes? I found this SO How to create “inline if

Differences in marking modified properties between DbSet.Attach() and DbSet.Update()

老子叫甜甜 提交于 2019-12-13 02:55:49
问题 let's say we have a Person class(with properties of Id and Age), let's say we need to change the Age of a person whose id is 1 from 29 to 30, and I use DbSet.Update() and DbSet.Attach() respectively: Person p = new Person(){ Id = 1 } var entity = context.Persons.Update(p); p.Age = 30; Console.WriteLine("entity state:" + entity.State); foreach (var modifiedProperty in entity.Properties.Where(p => p.IsModified)) { Console.Write($"The {modifiedProperty.Metadata.Name} property is marked as