https://github.com/daverbk/entity-practice
Entity Framework studies
https://github.com/daverbk/entity-practice
entityframework
Last synced: 12 months ago
JSON representation
Entity Framework studies
- Host: GitHub
- URL: https://github.com/daverbk/entity-practice
- Owner: daverbk
- Archived: true
- Created: 2022-08-29T14:35:32.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-06T09:01:34.000Z (12 months ago)
- Last Synced: 2025-04-06T10:19:07.437Z (12 months ago)
- Topics: entityframework
- Language: C#
- Homepage:
- Size: 36.2 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Entity Framework start flow
## 1. Create models
**Models represent database tables**
```csharp
class Dish
{
public int Id { get; set; }
[MaxLength(100)]
public string Title { get; set; } = string.Empty;
[MaxLength(1000)]
public string? Notes { get; set; }
public int? Starts { get; set; }
public List Ingredients { get; set; } = new();
}
class DishIngredient
{
public int Id { get; set; }
public string Description { get; set; } = string.Empty;
[MaxLength(50)]
public string UnitOfMeasure { get; set; } = string.Empty;
[Column(TypeName = "decimal(5, 2)")]
public decimal Amount { get; set; }
public Dish? Dish { get; set; }
public int DishId { get; set; }
}
```
## 2. Create a db context
**Represents connection string with db**
```csharp
class CookbookContext : DbContext
{
public DbSet Dishes { get; set; } = null!;
public DbSet Ingredients { get; set; } = null!;
public CookbookContext(DbContextOptions options) : base(options)
{ }
}
```
# 3. Create a db context factory
**We can use `appsettings.json` for the purposes of storing connection data**
```csharp
class CookbookContextFactory : IDesignTimeDbContextFactory
{
public CookbookContext CreateDbContext(string[]? args = null)
{
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder
.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole()))
.UseSqlServer(configuration["ConnectionStrings:DefaultConnection"]!);
return new CookbookContext(optionsBuilder.Options);
}
}
```
`appsettings.json` **file**
```json
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=CookbookLesson;User=sa;Password=***"
}
}
```
## 4. Install ef globally on your machine
`dotnet tool install --global dotnet-ef`
## 5. Migrations
**Migrations are needed to transfer data from C# to a database table.**
**From solution directory run `dotnet ef migrations add Initial` to add initial migration that will be responsible for converting C# instances to db table records.**
**To finally create a db run `dotnet ef database update`**
## **_NuGet packages to be installed:_**
`Microsoft.EntityFrameworkCore.SqlServer`
`Microsoft.Extensions.Configuration.Json`
`Microsoft.EntityFrameworkCore.Design`
`Microsoft.Extensions.Logging.Console`