Skip to content

Instantly share code, notes, and snippets.

@Taifunov Taifunov/dbContext
Created Aug 25, 2019

Embed
What would you like to do?
public class MyDbContext : DbContext
{
public DbSet<Car> Cars { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<CarMark> CarsMarks { get; set; }
public MyDbContext()
{
Database.EnsureCreated();
}
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Filename=dbTest.db", options =>
{
options.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName);
});
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlite("Filename=dbTest.db");
}
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>(entity =>
{
entity.HasKey(e => e.Id);
entity.HasIndex(e => e.Number).IsUnique();
entity.Property(e => e.Name);
entity.Property(e => e.City);
});
modelBuilder.Entity<Car>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Mark);
entity.Property(e => e.Model);
entity.HasIndex(e => e.Number).IsUnique();
entity.Property(e => e.Price);
});
modelBuilder.Entity<CarMark>(entity =>
{
entity.HasKey(e => e.Id);
entity.HasIndex(e => e.Mark).IsUnique();
});
base.OnModelCreating(modelBuilder);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.