/// <summary> /// ///Entity /// </summary> public class Product { public Product() { } public int Id { get; set; } public string Name { get; set; } public int Quantity { get; set; } public float Price { get; set; } } /// <summary> /// Entity /// geovindu, Geovin Du /// </summary> public class Customer { public Customer() { } public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } /// <summary> /// /// </summary> public class ECommerceDbContext : DbContext { public DbSet<Product> Products { get; set; } public DbSet<Customer> Customers { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("Server=DESKTOP-NQK85G5\\GEOVIN2008;Database=geovinduCoreDBA;User Id=sa;Password=geovindu;"); } /// <summary> /// /// </summary> /// <param name="modelBuilder"></param> protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Product>(); modelBuilder.Entity<Customer>(); base.OnModelCreating(modelBuilder); } } /// <summary> /// 初始化创建表格 /// </summary> [DbContext(typeof(ECommerceDbContext))] partial class ECommerceDbContextModelSnapshot : ModelSnapshot { /// <summary> /// /// </summary> /// <param name="modelBuilder"></param> protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 //ProductVersion modelBuilder .HasAnnotation("ProductVersion", "6.0.6") .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); //Customer modelBuilder.Entity("Customer", b => { b.Property<int>("Id") .ValueGeneratedOnAdd() .HasColumnType("int"); SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1); b.Property<string>("FirstName") .IsRequired() .HasColumnType("nvarchar(max)"); b.Property<string>("LastName") .IsRequired() .HasColumnType("nvarchar(max)"); b.HasKey("Id"); b.ToTable("Customers"); }); //Product modelBuilder.Entity("Product", b => { b.Property<int>("Id") .ValueGeneratedOnAdd() .HasColumnType("int"); SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1); b.Property<string>("Name") .IsRequired() .HasColumnType("nvarchar(max)"); b.Property<float>("Price") .HasColumnType("real"); b.Property<int>("Quantity") .HasColumnType("int"); b.HasKey("Id"); b.ToTable("Products"); }); #pragma warning restore 612, 618 } }
调用:
//初始化表格至数据库 ECommerceDbContext context = new(); await context.Database.MigrateAsync(); // context = new ECommerceDbContext(); List<Customer> customers = new List<Customer>(); //添加 Customer customer=new Customer(); customer.FirstName= "涂"; customer.LastName = "聚文"; customers.Add(customer); customer = new Customer(); customer.FirstName = "Du"; customer.LastName = "geovind"; customers.Add(customer); context.Customers.AddRange(customers); int k=context.SaveChanges(); if (k > 0) { Console.WriteLine("ok"); } //查询 var custer=context.Customers.FirstOrDefault(); Console.WriteLine(custer.FirstName+","+custer.LastName);
输出:
标签:donet,set,get,Entity,Framework,modelBuilder,Id,Property,public From: https://www.cnblogs.com/geovindu/p/17091262.html