ef-core-2.2

EF Core HasMany vs OwnsMany

冷暖自知 提交于 2020-08-06 07:52:31
问题 For one to many relationship, what's the difference between HasMany and OwnsMany ? When should I use one over another? For example: public class xxx { public virtual IReadOnlyCollection<xxxHistoryEntity> Histories => _histories; private readonly List<xxxHistoryEntity> _histories = new List<xxxHistoryEntity>(); } public class xxxHistoryEntity : Entity<string> { public string State { get; set; } public string NodeId { get; set; } public string Message { get; set; } } The Entity Configuration:

One-to-one and one-to-many relationships with same owned entity type

≡放荡痞女 提交于 2020-06-29 04:14:31
问题 After this question I tried working with owned entity types but I'm stuck at the following scenario: public class User { [Key] public Guid UserId { get; set; } public ICollection<Listing> Listings { get; set; } public Image Avatar { get; set; } } public class Listing { [Key] public Guid ListingId { get; set; } public ICollection<Image> Photos { get; set; } } [Owned] public class Image { public long Size { get; set; } public string Height { get; set; } public string Width { get; set; } }

Entity Framework Core throws System.Data.SqlTypes.SqlNullValueException when loading entities from DbContext

天涯浪子 提交于 2020-06-01 05:10:22
问题 I have this MyEntity table in the database: The data inside MyEntity is the following: The EF Core entity is the following: public class MyEntity { public int Id { get; set; } public int MyInt { get; set; } } I am getting an exception: System.Data.SqlTypes.SqlNullValueException: 'Data is Null. This method or property cannot be called on Null values.' when trying to load the MyEntity from the DbContext : var myEntities = dbContext.Set<MyEntity>.ToList(); What am I doing wrong? 回答1: You are

Ignore duplicate entries and commit successful ones on DbContext.SaveChanges() in EF Core

放肆的年华 提交于 2020-05-27 04:25:07
问题 I have an ASP .Net Core 2.2 Web API. In one of my controller actions, I am adding a bunch of rows to a MySQL database table (I'm using Pomelo). So for example: _dbContext.AddRange(entities); _dbContext.SaveChanges(); The entities I'm adding have two primary keys (composite primary key) and the keys are already populated in the entities collection when I add them to DbContext (i.e. I am setting the keys myself - there is no "auto increment" or anything like that where the database generates

Ignore duplicate entries and commit successful ones on DbContext.SaveChanges() in EF Core

扶醉桌前 提交于 2020-05-27 04:24:25
问题 I have an ASP .Net Core 2.2 Web API. In one of my controller actions, I am adding a bunch of rows to a MySQL database table (I'm using Pomelo). So for example: _dbContext.AddRange(entities); _dbContext.SaveChanges(); The entities I'm adding have two primary keys (composite primary key) and the keys are already populated in the entities collection when I add them to DbContext (i.e. I am setting the keys myself - there is no "auto increment" or anything like that where the database generates

Ignore duplicate entries and commit successful ones on DbContext.SaveChanges() in EF Core

丶灬走出姿态 提交于 2020-05-27 04:23:17
问题 I have an ASP .Net Core 2.2 Web API. In one of my controller actions, I am adding a bunch of rows to a MySQL database table (I'm using Pomelo). So for example: _dbContext.AddRange(entities); _dbContext.SaveChanges(); The entities I'm adding have two primary keys (composite primary key) and the keys are already populated in the entities collection when I add them to DbContext (i.e. I am setting the keys myself - there is no "auto increment" or anything like that where the database generates

EF Core 2.2 - Cannot project while using IQueryable

生来就可爱ヽ(ⅴ<●) 提交于 2020-05-17 08:50:27
问题 This is the method in my generic repository: public async Task<TEntity> GetByCondition(Expression<Func<TEntity, bool>> predicate, Func<DbSet<TEntity>, IQueryable<TEntity>> baseQuery = null) { IQueryable<TEntity> q = _context.Set<TEntity>(); if (baseQuery != null) { q = baseQuery(_context.Set<TEntity>()); } return await q.Where(predicate).FirstOrDefaultAsync(); } This is the usage in my business logic: Expression<Func<Countries, bool>> whereExpr = x => x.SlugHebrew == slug || x.SlugEnglish ==

Setup one to many relation with fk having multiple rows

不羁的心 提交于 2020-01-24 13:55:52
问题 I have a problem with setting up a one-to-many relation with EF Core. I have two tables address and address_country . There are schemas: As you see, I want to store countries with different locales. So it has a composed key. address table has a foreign key to address_country . Unfortunately, I cannot setup ContextDb to achieve what I want. The list of countries is not filled in Address model. I have the following code in Context : public class Context : DbContext { protected override void

Unable to update a record in asp.net-core 2.2 c#

我的未来我决定 提交于 2020-01-24 12:42:33
问题 I am trying to update the record of an item as follows [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Edit(int id, [Bind("Id,Label,Description,LessonId,Position,FileName,Format,Status")] Content content) { if (id != content.Id) { return NotFound(); } var existingContent = await _context.Contents.FindAsync(id).ConfigureAwait(false); if (existingContent == null) { return NotFound(); } string fileToDelete = null; if (ModelState.IsValid) { try { if (Request.Form.Files[

Unable to update a record in asp.net-core 2.2 c#

*爱你&永不变心* 提交于 2020-01-24 12:41:48
问题 I am trying to update the record of an item as follows [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Edit(int id, [Bind("Id,Label,Description,LessonId,Position,FileName,Format,Status")] Content content) { if (id != content.Id) { return NotFound(); } var existingContent = await _context.Contents.FindAsync(id).ConfigureAwait(false); if (existingContent == null) { return NotFound(); } string fileToDelete = null; if (ModelState.IsValid) { try { if (Request.Form.Files[