Last active
January 18, 2020 15:34
-
-
Save NMillard/3890911edcf16b7020e5e3871e4a263a to your computer and use it in GitHub Desktop.
EntityFramework examples
This file contains hidden or 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 EFPractice.DataLayer.Interfaces; | |
| using EFPractice.Domain; | |
| using Microsoft.EntityFrameworkCore; | |
| namespace EFPractice.DataLayer { | |
| internal class ApplicationContext : DbContext, IDataStore { | |
| public ApplicationContext(DbContextOptions options) : base(options) {} | |
| public DbSet<Author> Authors { get; set; } | |
| public DbSet<Book> Books { get; set; } | |
| protected override void OnModelCreating(ModelBuilder modelBuilder) { | |
| modelBuilder.ApplyConfigurationsFromAssembly(typeof(ApplicationContext).Assembly); | |
| } | |
| } | |
| } |
This file contains hidden or 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.ComponentModel.DataAnnotations; | |
| using System.ComponentModel.DataAnnotations.Schema; | |
| namespace EFPractice.Domain { | |
| /// <summary> | |
| /// Extreme case of a poorly designed class | |
| /// </summary> | |
| [Table("Books")] | |
| public class BadBook { | |
| public BadBook() { | |
| // Never have constructors that leave your objects in an invalid state. | |
| // Especially when it's just to please an external framework... | |
| } | |
| public BadBook(string name, DateTime released) { | |
| Name = name; | |
| Released = released; | |
| } | |
| [Key] | |
| public int Id { get; set; } | |
| [Required(ErrorMessage = "Book name is required")] | |
| [MaxLength(150)] | |
| public string Name { get; set; } | |
| [DataType("DateTime2")] | |
| public DateTime Released { get; set; } | |
| [Required] | |
| [ForeignKey("AuthorId")] | |
| public Author Author { get; set; } | |
| public string AuthorId { get; set; } | |
| } | |
| } |
This file contains hidden or 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; | |
| namespace EFPractice.Domain { | |
| public class Book { | |
| private string id; | |
| public Book(string name) { | |
| id = Guid.NewGuid().ToString("D"); | |
| Name = name; | |
| } | |
| public string Name { get; private set; } | |
| public DateTime Released { get; private set; } | |
| public Author Author { get; private set; } | |
| } | |
| } |
This file contains hidden or 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 EFPractice.Domain; | |
| using Microsoft.EntityFrameworkCore; | |
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | |
| namespace EFPractice.DataLayer.Configurations { | |
| public class BookConfig : IEntityTypeConfiguration<Book> { | |
| public void Configure(EntityTypeBuilder<Book> builder) { | |
| builder.ToTable("Books"); | |
| builder.HasKey("id"); | |
| builder.Property(book => book.Name).HasMaxLength(150).IsRequired(); | |
| builder.Property(book => book.Released).IsRequired(); | |
| builder.HasOne(book => book.Author) | |
| .WithMany(author => author.Books) | |
| .HasForeignKey("AuthorId") | |
| .IsRequired() | |
| .OnDelete(DeleteBehavior.Cascade); | |
| } | |
| } | |
| } |
This file contains hidden or 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 Microsoft.EntityFrameworkCore; | |
| using Microsoft.Extensions.DependencyInjection; | |
| namespace EFPractice.DataLayer.Extensions { | |
| public static class AddApplicationDbContext { | |
| public static IServiceCollection AddBookStoreDbContext(this IServiceCollection services, string connectionString) { | |
| services.AddDbContext<ApplicationContext>(builder => { | |
| builder.UseSqlServer(connectionString); | |
| }); | |
| return services; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment