Created
March 13, 2016 22:21
-
-
Save badcommandorfilename/471354120a988f283f93 to your computer and use it in GitHub Desktop.
NpgsqlEF7
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
public string ConnectionString = @"Port=5432;Server=127.0.0.1;Database=eftest;UserId=dbuser;Password=dbpassword;"; | |
public DbSet<Blog> Blogs { get; set; } | |
public DbSet<Post> Posts { get; set; } | |
protected override void OnModelCreating(ModelBuilder modelBuilder) | |
{ | |
// Make Blog.Url required | |
modelBuilder.Entity<Blog>() | |
.Property(b => b.url) | |
.IsRequired(); | |
modelBuilder.Entity<Blog>() | |
.Property(b => b.uuid) | |
.HasDefaultValueSql("uuid_generate_v1()"); | |
modelBuilder.Entity<Post>() | |
.Property(x => x.uuid) | |
.HasDefaultValueSql("uuid_generate_v1()"); | |
} | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
{ | |
var path = PlatformServices.Default.Application.ApplicationBasePath; | |
optionsBuilder.UseNpgsql(ConnectionString); | |
} | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
var ctx = new BloggingContext(); | |
services.AddEntityFramework() | |
.AddNpgsql() | |
.AddDbContext<BloggingContext>(options => options.UseNpgsql(ctx.ConnectionString)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment