dotnetcore EF (entity column definition, sql statement logging)
2022/1/29 19:09:01
本文主要是介绍dotnetcore EF (entity column definition, sql statement logging),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
class Bird { public int Number { get; set; } public string Name { get; set; } public int Age { get; set; } } class BirdConfig : IEntityTypeConfiguration<Bird> { public void Configure(EntityTypeBuilder<Bird> builder) { builder.ToTable("Bird"); // nvarchar is changed to varchar, but length is decided by HasMaxLength(50). builder.Property(e => e.Name).HasColumnName("bird_name").HasColumnType("nvarchar(80)").HasMaxLength(50).IsRequired(); builder.HasKey(e => e.Number).HasName("bird_number"); // change key name, not column name here. builder.Property(e => e.Number).ValueGeneratedOnAdd().IsRequired(); builder.Property(e => e.Age).HasDefaultValue(1); builder.HasIndex(e => e.Name).IsUnique(); builder.HasIndex(e => new { e.Name, e.Age }); } }
class Book { public int Id { get; set; } public string Name { get; set; } public string Author { get; set; } public int Age { get; set; } } class BookConfig : IEntityTypeConfiguration<Book> { public void Configure(EntityTypeBuilder<Book> builder) { builder.ToTable("Book"); //builder.ToView("book_view"); // nvarchar is changed to varchar, but length is decided by HasMaxLength(50). builder.Property(e => e.Name).HasColumnType("nvarchar(80)").HasMaxLength(50).IsRequired(); builder.Property(e => e.Author).HasColumnName("author_name").HasMaxLength(100).IsRequired(); builder.Ignore(e => e.Age); } }
class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public DateTime Birthday { get; set; } public override string ToString() { return $"Id:{Id}, Name:{Name}, Age:{Age}, Birthday:{Birthday}"; } } class PersonConfig : IEntityTypeConfiguration<Person> { public void Configure(EntityTypeBuilder<Person> builder) { builder.ToTable("Person"); builder.Property(e => e.Name).HasMaxLength(50).IsRequired(); } }
class MyDbContext : DbContext { public DbSet<Person> Persons { get; set; } public DbSet<Book> Books { get; set; } public DbSet<Bird> Birds { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); optionsBuilder.UseLoggerFactory(loggerFactory); // output sql script to log. //optionsBuilder.LogTo(Console.WriteLine); optionsBuilder.UseSqlServer("Server=.;Database=demo01;Trusted_Connection=True;MultipleActiveResultSets=True"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly); } }
class Program { static async Task Main(string[] args) { await ReadPersons(); } private static Task ReadPersons() { using (MyDbContext ctx = new MyDbContext()) { var persons = ctx.Persons.Where(p => p.Age > 50); //Console.WriteLine(persons.ToQueryString()); foreach (Person p in persons) { Console.WriteLine(p); } } return Task.CompletedTask; } private static async Task SavePersons() { Random random = new Random(); using (MyDbContext ctx = new MyDbContext()) { for (int i = 0; i < 5; i++) { Person p = new Person() { Name = $"Person_{i}", Age = random.Next(10, 90), }; ctx.Persons.Add(p); } await ctx.SaveChangesAsync(); } Console.WriteLine("done"); } }
这篇关于dotnetcore EF (entity column definition, sql statement logging)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#