An open API service indexing awesome lists of open source software.

https://github.com/egemenciftci/entity-framework-core-cheat-sheet

Entity Framework Core Cheat Sheet
https://github.com/egemenciftci/entity-framework-core-cheat-sheet

csharp entity-framework-core net

Last synced: 6 months ago
JSON representation

Entity Framework Core Cheat Sheet

Awesome Lists containing this project

README

          

# Entity Framework Core Cheat Sheet (EF Core 6.0)

| Configuration | Details |
| :--- | :--- |
| `modelBuilder.HasDefaultSchema("dbo");` | Set default schema. |
| `modelBuilder.Entity().ToTable("StudentInfo");` | Map an entity to database table in the default schema. |
| `modelBuilder.Entity().ToTable("StudentInfo", schema: "dbo");` | Map an entity to database table in a specific schema. |
| `modelBuilder.Entity().ToView("StudentsView", schema: "dbo");` | Map an entity to database view in a specific schema. |
| `modelBuilder.Entity().HasKey(s => s.Id);` | Configure primary key(s). |
| `modelBuilder.Entity().HasAlternateKey(s => s.Id);` | Configure alternate key(s). |
| `modelBuilder.Entity().HasIndex(s => s.Id);` | Configure index(es). |
| `modelBuilder.Entity().HasIndex(s => s.Id).IsUnique();` | Configure unique index(es). |
| `modelBuilder.Entity().HasIndex(s => s.Url).HasDatabaseName("Index_Url");` | Configure index name. |
| `modelBuilder.Entity().HasIndex(s => s.Url).HasFilter("[Url] IS NOT NULL");` | Configure index filter. |
| `modelBuilder.Entity().HasOne(s => s.Grade);` | Configures the One part of the relationship. |
| `modelBuilder.Entity().HasMany(s => s.Courses);` | Configures the Many part of the relationship. |
| `modelBuilder.Entity().HasComment("Some comment");` | Set text comment in the database. |
| `modelBuilder.Entity().Ignore(s => s.LoadedFromDatabase);` | Exclude a property. |
| `modelBuilder.Entity().HasCheckConstraint("CK_Prices", "[Price] > [DiscountedPrice]", c => c.HasName("CK_Product_Prices"));` | Configure check constraint. |
| `modelBuilder.HasSequence("OrderNumbers");`
`modelBuilder.Entity().Property(o => o.OrderNo).HasDefaultValueSql("NEXT VALUE FOR OrderNumbers");` | Configure a sequence. |
| `modelBuilder.HasSequence("OrderNumbers", schema: "shared").StartsAt(1000).IncrementsBy(5);` | Configure schema, start value and increment of a sequence. |
| `modelBuilder.Entity().Property(s => s.Name).HasColumnType("varchar(200)");` | Configure a column data type. |
| `modelBuilder.Entity().Property(s => s.Name).HasColumnName("Name");` | Configure a column name. |
| `modelBuilder.Entity().Property(s => s.Name).HasDefaultValue("StudentX");` | Configure a column default value. |
| `modelBuilder.Entity().Property(s => s.Created).HasDefaultValueSql("getdate()");` | Configure a column default value by SQL. |
| `modelBuilder.Entity().Property(s => s.LastModified).HasComputedColumnSql("GetUtcDate()");` | Configure a computed column. |
| `modelBuilder.Entity().Property(s => s.LastModified).HasComputedColumnSql("GetUtcDate()", stored: true);` | Configure a stored computed column. |
| `modelBuilder.Entity().Property(s => s.Surname).IsRequired();` | Configure a not null column. |
| `modelBuilder.Entity().Property(s => s.Name).HasMaxLength(50);` | Configure max length for a string or array type column. |
| `modelBuilder.Entity().Property(s => s.Weight).HasPrecision(5, 2);` | Configure precision and scale of the type. |
| `modelBuilder.Entity().Property(s => s.Name).IsUnicode(false);` | Configure a non-Unicode string column. |
| `modelBuilder.Entity().Property(s => s.Name).UseCollation("SQL_Latin1_General_CP1_CI_AS");` | Configure collation of a text column. |
| `modelBuilder.Entity().Property(s => s.Name).HasComment("Some comment");` | Set a column comment. |
| `modelBuilder.Entity().Property(s => s.Name).HasColumnOrder(1);` | Set column order. |
| `modelBuilder.Entity().Property(s => s.Inserted).ValueGeneratedOnAdd();` | Set value generation for a column on add. |
| `modelBuilder.Entity().Property(s => s.LastUpdated).ValueGeneratedOnAddOrUpdate();` | Set value generation for a column on add or update. |
| `modelBuilder.Entity().Property(s => s.Id).ValueGeneratedNever();` | Disable value generation. |
| `modelBuilder.Entity().Property(s => s.LastName).IsConcurrencyToken();` | Configure a concurrency token. |
| `modelBuilder.Entity().Property(s => s.Timestamp).IsRowVersion();` | Configure a property to be a timestamp/rowversion. |
| `modelBuilder.Entity().Property("LastUpdated");` | Configure a shadow property. |
| `modelBuilder.Entity().IndexerProperty("LastUpdated");` | Configure an indexer property. |
| `modelBuilder.Entity().Property(b => b.Url).HasField("_validatedUrl");` | Configure a backing field. |
| `modelBuilder.Entity().Property(e => e.Mount).HasConversion(v => v.ToString(), v => (EquineBeast)Enum.Parse(typeof(EquineBeast), v));` | Configure a value conversion. |
| `modelBuilder.Entity().HasData(new Blog { BlogId = 1, Url = "http://sample.com" });` | Configure seed data. |
| `modelBuilder.Entity().OwnsOne(p => p.ShippingAddress);` | Define an owned entity. |
| `modelBuilder.Entity().HasNoKey();` | Define a keyless entity. |

| Usage | Details |
| :--- | :--- |
| `using var context = new SchoolContext();`
`// Work with context here` | Create and use a database context. |
| `context.Add(new Student { Name = "SomeName" });`
`context.SaveChanges();` | Create. |
| `var entity = context.Students.FirstOrDefault(f => f.Name == "SomeName");` | Read. |
| `entity.Surname = "SomeSurname";`
`context.SaveChanges();` | Update. |
| `context.Remove(entity);`
`context.SaveChanges();` | Delete. |
| `context.RemoveRange(entities);`
`context.SaveChanges();` | Batch delete. |
| `context.Students.Where(s => s.FirstName == "Bill").Include(s => s.Grade).FirstOrDefault();` | Eager loading. |
| `context.Students.Where(s => s.FirstName == "Bill").Include(s => s.Grade).ThenInclude(g => g.Teachers);` | Multi level eager loading. |
| `var state = context.Entry(entity).State;` | Get the current state of the entity. |
| `var students = context.Students.FromSqlRaw("SELECT * FROM dbo.Students").ToList();` | Execute raw SQL. |
| `var students = context.Students.FromSqlInterpolated($"SELECT * FROM dbo.Students WHERE NAME={name}").ToList();` | Execute raw SQL with interpolated string. |