{"id":18624968,"url":"https://github.com/daverbk/entity-practice","last_synced_at":"2025-04-11T04:32:23.781Z","repository":{"id":114483864,"uuid":"530259239","full_name":"daverbk/entity-practice","owner":"daverbk","description":"Entity Framework studies","archived":true,"fork":false,"pushed_at":"2025-04-06T09:01:34.000Z","size":37966,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-06T10:19:07.437Z","etag":null,"topics":["entityframework"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/daverbk.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-29T14:35:32.000Z","updated_at":"2025-04-06T09:01:53.000Z","dependencies_parsed_at":"2023-05-16T22:45:36.730Z","dependency_job_id":null,"html_url":"https://github.com/daverbk/entity-practice","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daverbk%2Fentity-practice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daverbk%2Fentity-practice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daverbk%2Fentity-practice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daverbk%2Fentity-practice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/daverbk","download_url":"https://codeload.github.com/daverbk/entity-practice/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248345198,"owners_count":21088231,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["entityframework"],"created_at":"2024-11-07T04:32:14.759Z","updated_at":"2025-04-11T04:32:23.761Z","avatar_url":"https://github.com/daverbk.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Entity Framework start flow\n\n## 1. Create models \n**Models represent database tables**\n\n```csharp\n    class Dish\n    {\n        public int Id { get; set; }\n\n        [MaxLength(100)]\n        public string Title { get; set; } = string.Empty;\n\n        [MaxLength(1000)]\n        public string? Notes { get; set; }\n\n        public int? Starts { get; set; }\n\n        public List\u003cDishIngredient\u003e Ingredients { get; set; } = new();\n    }\n\n    class DishIngredient\n    {\n        public int Id { get; set; }\n\n        public string Description { get; set; } = string.Empty;\n    \n        [MaxLength(50)]\n        public string UnitOfMeasure { get; set; } = string.Empty;\n\n        [Column(TypeName = \"decimal(5, 2)\")]\n        public decimal Amount { get; set; }\n\n        public Dish? Dish { get; set; }\n\n        public int DishId { get; set; }\n    }\n```\n\n## 2. Create a db context \n**Represents connection string with db**\n\n```csharp\n    class CookbookContext : DbContext\n    {\n        public DbSet\u003cDish\u003e Dishes { get; set; } = null!;\n\n        public DbSet\u003cDishIngredient\u003e Ingredients { get; set; } = null!;\n\n        public CookbookContext(DbContextOptions\u003cCookbookContext\u003e options) : base(options)\n        { }\n    }\n```\n\n# 3. Create a db context factory\n**We can use `appsettings.json` for the purposes of storing connection data**\n\n```csharp\n    class CookbookContextFactory : IDesignTimeDbContextFactory\u003cCookbookContext\u003e\n    {\n        public CookbookContext CreateDbContext(string[]? args = null)\n        {\n            var configuration = new ConfigurationBuilder().AddJsonFile(\"appsettings.json\").Build();\n\n            var optionsBuilder = new DbContextOptionsBuilder\u003cCookbookContext\u003e();\n            optionsBuilder\n                .UseLoggerFactory(LoggerFactory.Create(builder =\u003e builder.AddConsole()))\n                .UseSqlServer(configuration[\"ConnectionStrings:DefaultConnection\"]!);\n\n            return new CookbookContext(optionsBuilder.Options);\n        }\n    }\n```\n\n`appsettings.json` **file**\n\n```json\n{\n  \"ConnectionStrings\": {\n    \"DefaultConnection\": \"Server=localhost;Database=CookbookLesson;User=sa;Password=***\"\n  }\n}\n```\n\n## 4. Install ef globally on your machine \n\n`dotnet tool install --global dotnet-ef`\n\n## 5. Migrations\n\n**Migrations are needed to transfer data from C# to a database table.**\n**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.**\n\n**To finally create a db run `dotnet ef database update`**\n\n## **_NuGet packages to be installed:_** \n\n`Microsoft.EntityFrameworkCore.SqlServer`\n\n`Microsoft.Extensions.Configuration.Json`\n\n`Microsoft.EntityFrameworkCore.Design`\n\n`Microsoft.Extensions.Logging.Console`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaverbk%2Fentity-practice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdaverbk%2Fentity-practice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaverbk%2Fentity-practice/lists"}