Posts

Showing posts with the label entity-framework-core

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 ...

Insert entity with many to many related entities which exist in the database

Insert entity with many to many related entities which exist in the database I have two objects with many to many relationship: public class Order { public int OrderID { get; set; } public string OrderName { get; set; } public virtual Collection<Product> Products { get; set; } = new Collection<Product>(); } public class Product { public int ProductID { get; set; } public string ProductName { get; set; } public int OrderID { get; set; } public virtual Collection<Order> Orders { get; set; } = new Collection<Order>(); } // Mapping table public class OrderProduct { public int OrderId { get; set; } public Order Order { get; set; } public int ProductId { get; set; } public Product Product { get; set; } } I would like to add a new Order with a collection of existing Products (my API would have an input of ProductID array) into the database, so I perform like: private void AddOrder(int productIDs) { Order newOrder = new Order(); ...

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 ...