数据库
首页 > 数据库> > 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");
        }
    }

标签:definition,set,logging,Name,get,column,builder,Age,public
来源: https://www.cnblogs.com/mryux/p/15854293.html