entity-framework-core

EF Core DB First, and how to avoid Constructor Overwrite on Model Generation

↘锁芯ラ 提交于 2021-01-19 06:23:20
问题 I am moving away from using SQL Authentication with my Azure DB, to Active Directory Managed Authentication as explained in this article. Basically, I'm doing two main things to get this working. 1- injecting the token in the DBContext constructor: public MyDBContext(DbContextOptions<MyDBContext> options) : base(options) { var conn = (SqlConnection)Database.GetDbConnection(); conn.AccessToken = (new Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProvider()).GetAccessTokenAsync(

Include several references on the second level

雨燕双飞 提交于 2021-01-16 04:54:32
问题 Assume we have this model : public class Tiers { public List<Contact> Contacts { get; set; } } and public class Contact { public int Id { get; set; } public Tiers Tiers { get; set; } public Titre Titre { get; set; } public TypeContact TypeContact { get; set; } public Langue Langue { get; set; } public Fonction Fonction { get; set; } public Service Service { get; set; } public StatutMail StatutMail { get; set; } } With EF7 I would like to retrieve all data from the Tiers table, with data from

Include several references on the second level

老子叫甜甜 提交于 2021-01-16 04:53:46
问题 Assume we have this model : public class Tiers { public List<Contact> Contacts { get; set; } } and public class Contact { public int Id { get; set; } public Tiers Tiers { get; set; } public Titre Titre { get; set; } public TypeContact TypeContact { get; set; } public Langue Langue { get; set; } public Fonction Fonction { get; set; } public Service Service { get; set; } public StatutMail StatutMail { get; set; } } With EF7 I would like to retrieve all data from the Tiers table, with data from

Using Fody Costura with Entity Framework Core migrations

北战南征 提交于 2021-01-07 12:58:56
问题 My WPF program is using Entity Framewok Core 3.1.1 (code-first) with a SQLite database. If the database file does not exist on startup, the program calls context.Database.Migrate() to create one. Works great. When I added Costura.Fody (using NuGet), that huge collection of DLLs disappeared and the program still works - until it needs to create a new database file. Then the Migrate() function fails with an error: The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an

Using Fody Costura with Entity Framework Core migrations

时光毁灭记忆、已成空白 提交于 2021-01-07 12:51:43
问题 My WPF program is using Entity Framewok Core 3.1.1 (code-first) with a SQLite database. If the database file does not exist on startup, the program calls context.Database.Migrate() to create one. Works great. When I added Costura.Fody (using NuGet), that huge collection of DLLs disappeared and the program still works - until it needs to create a new database file. Then the Migrate() function fails with an error: The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an

Mapping NULLs to type default

点点圈 提交于 2021-01-07 02:32:39
问题 I have model classes that I need to use with Entity Framework Core. Unfortunately, I cannot make changes to these classes and none of them use nullable columns, while the database tables they map to do have nullable columns. EF throws an (expected) exception when trying to pull data from the database into the model class. Is there a way to configure EF Core to automatically map NULLs in a column to the default for the given type? For example, //Model class class Foo { public int Id {get; set;

Ignore global query filter for joined entities

微笑、不失礼 提交于 2021-01-05 07:10:09
问题 Global query filters are very handy when implementing tenant and soft deletion features. But my problem is is that when i write a query with joins, for instance dbContext .entity1 .Include("entity2.entity3.entity4") .Where(something) .select(something) .toList(); and every one of those entities has global filters, then in the generated SQL i get for every JOIN a full Subquery where it selects all fields of that entity and checks for the global filters. But i dont want that. I want the global

Ignore global query filter for joined entities

喜夏-厌秋 提交于 2021-01-05 07:09:11
问题 Global query filters are very handy when implementing tenant and soft deletion features. But my problem is is that when i write a query with joins, for instance dbContext .entity1 .Include("entity2.entity3.entity4") .Where(something) .select(something) .toList(); and every one of those entities has global filters, then in the generated SQL i get for every JOIN a full Subquery where it selects all fields of that entity and checks for the global filters. But i dont want that. I want the global

EF Core multiple navigation properties produce circular dependency

女生的网名这么多〃 提交于 2021-01-04 06:09:49
问题 I have the following mapping configuration: Entry-Class: entity .HasOne(e => e.CurrentHandling) .WithOne() .HasForeignKey<Entry>(e => e.CurrentHandlingID) ; entity .HasMany(e => e.Handlings) .WithOne(h => h.Entry) .HasForeignKey(h => h.EntryID) ; Handling-Class: entity .HasOne(h => h.Entry) .WithMany(e => e.Handlings) .HasForeignKey(h => h.EntryID) .OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Restrict); ERM: When I try to save the context I get the following exception:

EF Core multiple navigation properties produce circular dependency

一笑奈何 提交于 2021-01-04 06:08:57
问题 I have the following mapping configuration: Entry-Class: entity .HasOne(e => e.CurrentHandling) .WithOne() .HasForeignKey<Entry>(e => e.CurrentHandlingID) ; entity .HasMany(e => e.Handlings) .WithOne(h => h.Entry) .HasForeignKey(h => h.EntryID) ; Handling-Class: entity .HasOne(h => h.Entry) .WithMany(e => e.Handlings) .HasForeignKey(h => h.EntryID) .OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Restrict); ERM: When I try to save the context I get the following exception: