-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
8,861 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace EntityFramework; | ||
|
||
public class Category | ||
{ | ||
public int CategoryId { get; set; } | ||
|
||
[Required] | ||
[StringLength(15)] | ||
public string? CategoryName { get; set; } | ||
|
||
[Column(TypeName = "ntext")] | ||
public string? Description { get; set; } | ||
|
||
public virtual ICollection<Product> Products { get; set; } | ||
|
||
public Category() | ||
{ | ||
Products = new HashSet<Product>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.12" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.12"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.12" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace EntityFramework; | ||
|
||
public class Northwind : DbContext | ||
{ | ||
public DbSet<Category> Categories { get; set; } | ||
public DbSet<Product> Products { get; set; } | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
string path = Path.Combine(Environment.CurrentDirectory, "Northwind.db"); | ||
optionsBuilder.UseSqlite($"Filename={path}"); | ||
} | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
|
||
} | ||
} |
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace EntityFramework; | ||
|
||
public class Product | ||
{ | ||
public int ProductId { get; set; } | ||
|
||
[Required] | ||
[StringLength(40)] | ||
public string? ProductName { get; set; } | ||
|
||
public int SupplierId { get; set; } | ||
public int CategoryId { get; set; } | ||
|
||
[Column("UnitPrice", TypeName = "money")] | ||
public decimal? UnitPrice { get; set; } | ||
|
||
[Column("UnitsInStock")] | ||
public short? UnitsInStock { get; set; } | ||
|
||
[Column("UnitsOnOrder")] | ||
public short? UnitsOnOrder { get; set; } | ||
|
||
[Column("ReorderLevel")] | ||
public short? ReorderLevel { get; set; } | ||
|
||
[Required] | ||
public bool Discontinued { get; set; } | ||
|
||
public virtual Category Category { get; set;} = null!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using EntityFramework; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
public class Program | ||
{ | ||
static void Main() | ||
{ | ||
using(Northwind db = new Northwind()) | ||
{ | ||
Console.WriteLine($"Name: {db.Database}"); | ||
Console.WriteLine($"Database: {db.Database.ProviderName}"); | ||
Console.WriteLine($"Database: {db.Database.CanConnect()}"); | ||
|
||
var categories = db.Categories.Where(cat => cat.CategoryId == 1).Include(cat => cat.Products); | ||
foreach (var cat in categories) | ||
{ | ||
Console.WriteLine(cat.CategoryName); | ||
foreach (var product in cat.Products) | ||
{ | ||
Console.WriteLine(" {0}", product.ProductName); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace EntityFramework; | ||
|
||
public class Supplier | ||
{ | ||
public int SupplierId { get; set; } | ||
|
||
[Required] | ||
[StringLength(40)] | ||
public string? CompanyName { get; set; } | ||
} |