Net Core教程

dotnetcore EF (entity column definition, sql statement logging)

本文主要是介绍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)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!