Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- class Program
- {
- static async Task Main()
- {
- var options = new DbContextOptionsBuilder();
- options.UseInMemoryDatabase("Widgets");
- using var db = new WidgetContext(options.Options);
- db.Set<Widget>().AddRange(
- new Widget { Name = "Foo" },
- new Widget { Name = "Bar" },
- new Widget { Name = "Fizz", Tag = "fizz" }
- );
- await db.SaveChangesAsync();
- var query = db.Set<Widget>().Where(w => w.Tag == "fizz");
- foreach (var w in await query.ToListAsync())
- {
- Console.WriteLine(w.Name);
- }
- }
- }
- public class Widget
- {
- public int Id { get; set; }
- [Required(AllowEmptyStrings = false)]
- public string Name { get; set; }
- internal string Tag { get; set; }
- }
- class WidgetContext : DbContext
- {
- public WidgetContext(DbContextOptions options)
- : base(options)
- {
- }
- protected override void OnModelCreating(ModelBuilder modelBuilder)
- {
- base.OnModelCreating(modelBuilder);
- modelBuilder.Entity<Widget>();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement