37
loading...
This website collects cookies to deliver better user experience
npgsql.entityframeworkcore.postgresql
which will allow us to make the CRUD methods more similar to the EFCore ORM we are used to.npgsql.entityframeworkcore.postgresql
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Toll
EFCore.NamingConventions
ConfigureServices Method
services.AddDbContext<PostgresNsql.MyContext>(option => option.UseNpgsql(Configuration.GetConnectionString("Nsql")));
public class MyContext : DbContext
{
public MyContext(DbContextOptions options) :base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseNpgsql()
.UseSnakeCaseNamingConvention();
protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder
.Entity<DataModel>()
.Property(e => e.Id)
.UseIdentityAlwaysColumn();
public DbSet<DataModel> DataModels { get; set; }
}
UseSnakeCaseNamingConvention()
Snake Case may be needed to handle JSONs that separate 2 words with _ underscore. That way you won't have problems with serialization..Property(e => e.Id)
.UseIdentityAlwaysColumn();
Identity always column is one of the types of value assignment you can give, in this mode you define that the values will be generated directly in the database, making it impossible for you to set the value at the time of insertion into the database.System.Text.Json
library and type its property with the JsonDocument class, as it will already identify that data will be received there in JSON and will facilitate de-cerealization.public class DataModel : IDisposable
{
public int Id { get; set; }
public string Nome { get; set; }
public int idade { get; set; }
public JsonDocument Data { get; set; }
public void Dispose() => Data?.Dispose();
}
[Column(TypeName = "jsonb")]
to identify the Json object and map the properties.public class DataModel
{
public int Id { get; set; }
public string Nome { get; set; }
public int idade { get; set; }
[Column(TypeName = "jsonb")]
public Endereco Address { get; set; }
}
public class Endereco // Mapping the JSON Data received
{
public string Rua { get; set; }
public int Numero { get; set; }
public Casa[] Casas { get; set; }
}
public class Casa
{
public decimal Preco { get; set; }
public string EnderecoCompleto { get; set; }
}
public List<DataModel> GetAll()
{
return _myContext.DataModels.ToList();
}
public IEnumerable<string> Get()
{
var resultado = _myContext.DataModels;
return resultado.Where(x => x.idade == 12 && x.Data.RootElement.GetProperty("lastName").GetString() == "Paixao").ToList();
}
public IEnumerable<string> Get()
{
var resultado = _myContext.DataModels;
return resultado.Select(x=>x.Data.RootElement.GetProperty("lastName").GetString()).ToList();
}
RootElement
is a class that will get the JSON data from the Data
property allowing you to then name with the GetProperty("property")
method which JSON property you will work with and finally you must put the GetString method ()
to convert the JSON data you filtered to string.