Posts

Showing posts with the label entity-framework

Turning off shadow property generation

Image
Turning off shadow property generation I'm having a weird issue with setting Entity Framework Core foreign keys. EF keeps adding a shadow property automatically for my property and is creating a foreign key of it. That would be perfectly fine for me, however - I want to be able to set the foreign key delete behaviour to cascade - using that "automatic" shadow property I'm not allowed to do that. Therefore I decided to create my own foreign key with the Fluent API: modelBuilder.Entity<PostDataModel>(e => { // Primary key e.HasKey(c => c.Id); // Relation e.HasOne<PostGroupDataModel>() .WithMany() .HasForeignKey("GroupId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); }); This however, did not help much - automatically generated shadow property is still being generated for the table ( GroupId1 ): GroupId1 public class PostGroupDataModel { public int Id { get; set; } public string Name ...

Comparing to booleans gives false when they have the same value

Image
Comparing to booleans gives false when they have the same value I'm trying to implement an audit table using Entity Framework core 2.0. However, all the fields are being saved, not just the edited ones. I thought I could do a simple if statement. If the old value is not equal to the new value then it is a changed value. However, this is not working for booleans. Two true booleans are coming out as not equal. Here is my very simple code: if (property.OriginalValue != property.CurrentValue) { auditEntry.OldValues[propertyName] = property.OriginalValue; auditEntry.NewValues[propertyName] = property.CurrentValue; } Hopefully, you can see in the image that both OldValues and NewValues are true but it drops into the if statement when it shouldn't. I would like an if statement that compares strings, ints, booleans etc if possible. OldValues NewValues if i...

Is there a shorter, better and optimised way to fill this list from SQL database

Is there a shorter, better and optimised way to fill this list from SQL database I got this code and it works without problems. But i sense there is much better way to do this. namespace Repositories { public class AuthorRepository : IAuthorRepository { public List<Author> GetAllFromRepo() { using (AppContext myDB = new AppContext()) { List<Author> authorsFromRepo = new List<Author>(); foreach (var item in myDB.Authors) { authorsFromRepo.Add(new Author() { Books = new List<Book>(), ID = item.ID, FirstName = item.FirstName, LastName = item.LastName }); } return authorsFromRepo.ToList(); } ...

EF: Integer auto increment primary key

EF: Integer auto increment primary key Previously I have been working only with MySQL databases, it was quite a standard to use auto increment integer settings for the id fields. id Trying Entity Framework with SQLite database, I have noticed that EF is using a Guid structure identifiers by default and therefore requires string for such fields. In the result, we have a identifier looking like that: SQLite Guid string a75d9d3d-6543-44fe-9fb8-f19f411503e5 a75d9d3d-6543-44fe-9fb8-f19f411503e5 public class AccountDataModel { /// <summary> /// The unique Id /// </summary> public string Id { get; set; } [...] } Two questions: 2 Answers 2 Simply change public string Id { get; set; } to public int Id { get; set; } and should change your Identity column type int with seed as 1. Use EF migrations to see the code generated for creating tables, keys and relationships. If you ...

Entity Framework and PostgreSQL: quotation marks issue

Image
Entity Framework and PostgreSQL: quotation marks issue The problem I am facing is around SQL queries in pgAdmin 4. Entity Framework (including its Core edition) capitalizes the names of tables and columns. Which means your SQL will then look something like select e."Id", e."Text" from "Entries" e where e."Text" like '%implicated%' I was googling the way to prevent Entity Framework from capitalizing names but didn't found out much. Is there a workaround to avoid wrapping table and column names in quotes? Thanks in advance! Can you try [Column(“lowerCaseolumnName”)]? – vivek nuna Jun 14 at 18:22 1 Answer 1 Easy done! Go to OnModelCreating method. OnModelCreating You need an extension method (the code is ...