-
延时加载
int[] arr = { 1,2,3,4,5,6,7,8}; #region 延时加载 //linq语句在这里不会执行。如果不延时加载,直接toList()就行了。 var res = from i in arr where i > 3 select i; //在foreach的时候才会执行;相当于 foreach (var item in from i in arr where i > 3 select i){}。并且foreach执行的结构不会缓存,下次foreach执行res的时候,需要重新执行。这就是延时加载;如果不延时加载,直接toList()就行了。 foreach (var item in res) { Console.WriteLine(item); } Console.WriteLine("--------------------------------------------"); //改变index=0位置的元素后,会在foreach中再次执行res,此时输出的第一个元素是9; arr[0] = 9; foreach (var item in res) { Console.WriteLine(item); } #endregion Console.ReadKey();
-
EFCore
-
CodeFirst
Context文件如下:
public class EFCoreContext : DbContext { /// <summary> /// 无参构造函数是必须的 /// </summary> public EFCoreContext() { } public EFCoreContext(DbContextOptions<EFCoreContext> options) : base(options) { } public DbSet<Users> Users { get; set; } public DbSet<Departments> Departments { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("server=.;database=EFCoreDb;uid=sa;pwd=sunwoda@2020"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { //这里可以设置表的字段的属性,比如长度,是否非空;优先级高于model上的数据模型验证 base.OnModelCreating(modelBuilder); } }
相关命令:1.add-migartion "备注" ; 2. update-database ;