Code snippets for the blog article at 2016/11/09/entity-framework-core-probing-your-databases
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
dotnet restore | |
dotnet ef migrations add InitialMigration | |
dotnet ef database update |
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
// Circle implements IShape | |
public interface Circle : IShape | |
{ | |
public void Draw() | |
{ | |
// code to draw the shape here | |
} | |
public void Rotate(int degrees) | |
{ | |
// rotating circles is hard | |
return; | |
} | |
public void Scale(int scaleMultiplier) | |
{ | |
// code to scale here | |
} | |
} |
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
// the namespace with our data models - more on those in a moment | |
using CustomModels; | |
// the Entity Framework namespace | |
using Microsoft.EntityFrameworkCore; | |
namespace DatabaseContexts | |
{ | |
public class DatabaseContext : DbContext | |
{ | |
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options) { } | |
public DatabaseContext() { } | |
// DbSet of our Product class | |
public DbSet<Product> Products { get; set; } | |
} | |
} |
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
var user = context.Users.GetOrCreate(userId); | |
using Microsoft.EntityFrameworkCore; |
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
SELECT TOP 1 * FROM PRODUCTS WHERE ProductId = key |
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
using System; | |
using System.Collections.Generic; | |
using Microsoft.EntityFrameworkCore.Migrations; | |
namespace MyApp.Migrations | |
{ | |
public partial class InitialMigration : Migration | |
{ | |
protected override void Up(MigrationBuilder migrationBuilder) | |
{ | |
} | |
protected override void Down(MigrationBuilder migrationBuilder) | |
{ | |
} | |
} | |
} |
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
using CustomModels; | |
// for IEnumerable<T> | |
using System.Collections.Generic; | |
namespace MyRepositories | |
{ | |
public interface IProductRepository | |
{ | |
void AddOrUpdate(Product item); | |
IEnumerable<Product> GetAll(); | |
Product Find(int key); | |
void Remove(int key); | |
void Update(Product item); | |
} | |
} |
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
public interface IShape | |
{ | |
void Draw(); | |
void Rotate(int degrees); | |
void Scale(int scaleMultiplier); | |
} |
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
using CustomModels; | |
using MyRepositories; | |
using System; | |
using Microsoft.AspNetCore.Mvc; | |
namespace MyApp.Controllers | |
{ | |
[RouteAttribute("/")] | |
public class MyController : Controller | |
{ | |
private IRepository _repository { get; set; } | |
public MyController(IRepository repository) | |
{ | |
_repository = repository; | |
} | |
} | |
} | |
[HttpPost] | |
public string AddNewProduct() | |
{ | |
_repository.AddOrUpdate(new Product{ | |
ProductName = "Classic Widget", | |
ProductDescription = "A Classic Widget - does not contain any special add ons", | |
Created = DateTime.Now, | |
Modified = DateTime.Now | |
}); | |
return "Added new 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
namespace CustomModels | |
{ | |
public class Product | |
{ | |
// our primary key | |
public int ProductId { get; set; } | |
public string ProductName { get; set; } | |
public string ProductDescription { get; set; } | |
// audit data | |
public DateTime Created { get; set; } | |
public DateTime Modified { get; set; } | |
} | |
} |
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
using DatabaseContexts; | |
using CustomModels; | |
using Microsoft.EntityFrameworkCore; | |
// for IEnumerable<T> | |
using System.Collections.Generic; | |
// for LINQ things | |
using System.Linq; | |
namespace MyRepositories | |
{ | |
public class ProductRespository : IProductRepository | |
{ | |
// Dependency injection will happen here. The DatabaseContext | |
// class will be injected into the constructor from the | |
// IServicesCollection - more on that later | |
private DatabaseContext _context; | |
public ProductRespository(DatabaseContext context) | |
{ | |
_context = context; | |
} | |
public void AddOrUpdate(Product item) | |
{ | |
var databaseEntity = _context.Find(Product.ProductId); | |
// If we can't find an instance of the product | |
// in the database, then we need to add it to | |
// the DbSet. | |
// Assumption: a Product with an empty name is a new instance | |
if (_string.IsNullOrEmpty(databaseEntity.ProductName)) | |
{ | |
_context.Products.Add(item); | |
} | |
// Otherwise we want to update it | |
else | |
{ | |
Update(item); | |
} | |
_context.SaveChanges(); | |
} | |
public Product Find(int key) | |
{ | |
return _context.Products | |
.Where(prod => prod.ProductId == key) | |
.SingleOrDefault(); | |
} | |
public IEnumerable<Product> GetAll() | |
{ | |
return _context.Products.AsNoTracking(); | |
} | |
public void Remove(int key) | |
{ | |
var itemToRemove = _context.Products | |
.SingleOrDefault(prod => prod.ProductId == key); | |
if (itemToRemove != null) | |
{ | |
_context.Products.Remove(itemToRemove); | |
_context.SaveChanges(); | |
} | |
} | |
public void Update(Product item) | |
{ | |
var itemToUpdate = _context.Product | |
.SingleOrDefault(prod => prod.ProductId == item.ProductId); | |
if (itemToUpdate != null) | |
{ | |
// TODO: add code to update entity fields here | |
_context.SaveChanges(); | |
} | |
} | |
} | |
} |
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
"dependencies": { | |
"Microsoft.EntityFrameworkCore": "1.0.0", | |
"Microsoft.EntityFrameworkCore.Sqlite": "1.0.0", | |
"Microsoft.EntityFrameworkCore.Sqlite.Design": { | |
"version": "1.0.0", | |
"type": "build" | |
}, | |
"Microsoft.EntityFrameworkCore.Tools": { | |
"version": "1.0.0-preview2-final", | |
"type": "build" | |
} | |
}, | |
"tools": { | |
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final", | |
} | |
"ConnectionStrings": { | |
"DefaultConnection": "Data Source=myDatabase.db" | |
} |
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
// need to reference Entity Framework | |
using Microsoft.EntityFrameworkCore; | |
// also need to reference our contexts namespace | |
using DatabaseContexts; | |
namespace MyApp | |
{ | |
public class Startup | |
{ | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
// Add a reference to our DatabaseContext class as a DbContext. | |
// Use our DefaultConnection string from the appsettings.json | |
services.AddDbContext<DatabaseContext>(options => | |
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"))); | |
} | |
} | |
} | |
// we need to reference our Respositories | |
using MyRepositories; | |
namespace MyApp | |
{ | |
public class Startup | |
{ | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
// DI our interfaces into out controllers | |
services.AddTransient<IProductRepository, ProductRespository>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment