sql:
create view ViewEmployeeMasters as select a.*,b.Name as 'DepartmentName',b.Code as 'DepartmentCode' from EmployeeMasters as a,DepartmentMaster as b where a.DepartmentId=b.DepartmentId go
/// <summary> /// 视图 /// Create A Database View Using The Entity Framework (EF) Code First Approach /// </summary> public class ViewEmployeeMasters { public int EmployeeId { get; set; } /// <summary> /// /// </summary> public string Code { get; set; } /// <summary> /// /// </summary> public string Name { get; set; } /// <summary> /// /// </summary> public int DepartmentId { get; set; } /// <summary> /// /// </summary> public string DepartmentCode { get; set; } /// <summary> /// /// </summary> public string DepartmentName { get; set; } /// <summary> } /// <summary> /// 视图 /// </summary> public DbSet<ViewEmployeeMasters> ViewEmployeeMasters { get; set; } /// <summary> /// /// </summary> /// <param name="optionsBuilder"></param> protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { //读取配置文件 var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) //Environment.CurrentDirectory .AddJsonFile("appsettings.json") .AddInMemoryCollection() .Build(); var connStr = config["ConnectionStrings:GeovinDuDbContext"]; // optionsBuilder.UseSqlServer("Server=DESKTOP-NQK85G5\\GEOVIN2008;Database=geovindu;User Id=sa;Password=geovindu;"); optionsBuilder.UseSqlServer(connStr); } /// <summary> /// https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew /// 存储过程映射 /// Stored procedure mapping /// </summary> /// <param name="modelBuilder"></param> protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<DuProduct>(); modelBuilder.Entity<DuCustomer>(); modelBuilder.Entity<People>(); modelBuilder.Entity<ViewEmployeeMasters>().ToView("ViewEmployeeMasters") .HasNoKey(); }
调用:
DuDbContext context = new DuDbContext(); var viewEmployeeMasters = context.ViewEmployeeMasters .Where(po => po.EmployeeId < 10) .ToList(); foreach(ViewEmployeeMasters em in viewEmployeeMasters) { Console.WriteLine(em.Name+" 视图查询"); }
输出:
Security Warning: The negotiated TLS 1.0 is an insecure protocol and is supported for backward compatibility only. The recommended protocol version is TLS 1.2 and later. 涂聚文 视图查询 ok edit,ok
标签:donet,set,7.02,get,database,Entity,modelBuilder,ViewEmployeeMasters,public From: https://www.cnblogs.com/geovindu/p/17100058.html