首页 > 其他分享 >CSharp: donet 6 create view with EF Core 6

CSharp: donet 6 create view with EF Core 6

时间:2023-02-04 19:55:06浏览次数:41  
标签:OrderId donet Description Core PersonId create ... new Price

 

[Keyless]
public class PersonOrderCount
{
    public string Name { get; set; }
    public int Count { get; set; }
}
public class Person
{
    public int PersonId { get; set; }
    public string Name { get; set; }

    public ICollection<Order> Orders { get; set; }
}
public class Order
{
    public int OrderId { get; set; }
    public int PersonId { get; set; }
    public string Description { get; set; }
    public int Price { get; set; }

    public Person Person { get; set; }
}


class ApplicationDbContext : DbContext
{
    public DbSet<Person> Persons { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<PersonOrderCount> PersonOrderCounts { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());

        modelBuilder.Entity<Person>()
            .HasMany(p => p.Orders)
            .WithOne(o => o.Person)
            .HasForeignKey(o => o.PersonId);
        //创建视图
        modelBuilder.Entity<PersonOrderCount>()
            .HasNoKey()
            .ToView("vw_PersonOrderCount");
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server=DESKTOP-NQK85G5\\GEOVIN2008;Database=geovinduDBA;User ID=SA;Password=geovindu;TrustServerCertificate=True");
    }
}

  [DbContext(typeof(ApplicationDbContext))]
    partial class ApplicationDbContextModelSnapshot : ModelSnapshot
    {
        protected override void BuildModel(ModelBuilder modelBuilder)
        {
#pragma warning disable 612, 618
            modelBuilder
                .HasAnnotation("ProductVersion", "7.0.0")
                .HasAnnotation("Relational:MaxIdentifierLength", 128);

            SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

            modelBuilder.Entity("Order", b =>
                {
                    b.Property<int>("OrderId")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("int");

                    SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("OrderId"));

                    b.Property<string>("Description")
                        .IsRequired()
                        .HasColumnType("nvarchar(max)");

                    b.Property<int>("PersonId")
                        .HasColumnType("int");

                    b.Property<int>("Price")
                        .HasColumnType("int");

                    b.HasKey("OrderId");

                    b.HasIndex("PersonId");

                    b.ToTable("Orders");

                    b.HasData(
                        new
                        {
                            OrderId = 1,
                            Description = "...",
                            PersonId = 1,
                            Price = 10
                        },
                        new
                        {
                            OrderId = 2,
                            Description = "...",
                            PersonId = 2,
                            Price = 20
                        },
                        new
                        {
                            OrderId = 3,
                            Description = "...",
                            PersonId = 4,
                            Price = 30
                        },
                        new
                        {
                            OrderId = 4,
                            Description = "...",
                            PersonId = 5,
                            Price = 40
                        },
                        new
                        {
                            OrderId = 5,
                            Description = "...",
                            PersonId = 1,
                            Price = 50
                        },
                        new
                        {
                            OrderId = 6,
                            Description = "...",
                            PersonId = 6,
                            Price = 60
                        },
                        new
                        {
                            OrderId = 7,
                            Description = "...",
                            PersonId = 7,
                            Price = 70
                        },
                        new
                        {
                            OrderId = 8,
                            Description = "...",
                            PersonId = 1,
                            Price = 80
                        },
                        new
                        {
                            OrderId = 9,
                            Description = "...",
                            PersonId = 8,
                            Price = 90
                        },
                        new
                        {
                            OrderId = 10,
                            Description = "...",
                            PersonId = 9,
                            Price = 100
                        },
                        new
                        {
                            OrderId = 11,
                            Description = "...",
                            PersonId = 1,
                            Price = 110
                        },
                        new
                        {
                            OrderId = 12,
                            Description = "...",
                            PersonId = 2,
                            Price = 120
                        },
                        new
                        {
                            OrderId = 13,
                            Description = "...",
                            PersonId = 2,
                            Price = 130
                        },
                        new
                        {
                            OrderId = 14,
                            Description = "...",
                            PersonId = 3,
                            Price = 140
                        },
                        new
                        {
                            OrderId = 15,
                            Description = "...",
                            PersonId = 1,
                            Price = 150
                        },
                        new
                        {
                            OrderId = 16,
                            Description = "...",
                            PersonId = 4,
                            Price = 160
                        },
                        new
                        {
                            OrderId = 17,
                            Description = "...",
                            PersonId = 1,
                            Price = 170
                        },
                        new
                        {
                            OrderId = 18,
                            Description = "...",
                            PersonId = 1,
                            Price = 180
                        },
                        new
                        {
                            OrderId = 19,
                            Description = "...",
                            PersonId = 5,
                            Price = 190
                        },
                        new
                        {
                            OrderId = 20,
                            Description = "...",
                            PersonId = 6,
                            Price = 200
                        },
                        new
                        {
                            OrderId = 21,
                            Description = "...",
                            PersonId = 1,
                            Price = 210
                        },
                        new
                        {
                            OrderId = 22,
                            Description = "...",
                            PersonId = 7,
                            Price = 220
                        },
                        new
                        {
                            OrderId = 23,
                            Description = "...",
                            PersonId = 7,
                            Price = 230
                        },
                        new
                        {
                            OrderId = 24,
                            Description = "...",
                            PersonId = 8,
                            Price = 240
                        },
                        new
                        {
                            OrderId = 25,
                            Description = "...",
                            PersonId = 1,
                            Price = 250
                        },
                        new
                        {
                            OrderId = 26,
                            Description = "...",
                            PersonId = 1,
                            Price = 260
                        },
                        new
                        {
                            OrderId = 27,
                            Description = "...",
                            PersonId = 9,
                            Price = 270
                        },
                        new
                        {
                            OrderId = 28,
                            Description = "...",
                            PersonId = 9,
                            Price = 280
                        },
                        new
                        {
                            OrderId = 29,
                            Description = "...",
                            PersonId = 9,
                            Price = 290
                        },
                        new
                        {
                            OrderId = 30,
                            Description = "...",
                            PersonId = 2,
                            Price = 300
                        },
                        new
                        {
                            OrderId = 31,
                            Description = "...",
                            PersonId = 3,
                            Price = 310
                        },
                        new
                        {
                            OrderId = 32,
                            Description = "...",
                            PersonId = 1,
                            Price = 320
                        },
                        new
                        {
                            OrderId = 33,
                            Description = "...",
                            PersonId = 1,
                            Price = 330
                        },
                        new
                        {
                            OrderId = 34,
                            Description = "...",
                            PersonId = 1,
                            Price = 340
                        },
                        new
                        {
                            OrderId = 35,
                            Description = "...",
                            PersonId = 5,
                            Price = 350
                        },
                        new
                        {
                            OrderId = 36,
                            Description = "...",
                            PersonId = 1,
                            Price = 360
                        },
                        new
                        {
                            OrderId = 37,
                            Description = "...",
                            PersonId = 5,
                            Price = 370
                        },
                        new
                        {
                            OrderId = 38,
                            Description = "...",
                            PersonId = 1,
                            Price = 380
                        },
                        new
                        {
                            OrderId = 39,
                            Description = "...",
                            PersonId = 1,
                            Price = 390
                        },
                        new
                        {
                            OrderId = 40,
                            Description = "...",
                            PersonId = 1,
                            Price = 400
                        });
                });

            modelBuilder.Entity("Person", b =>
                {
                    b.Property<int>("PersonId")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("int");

                    SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("PersonId"));

                    b.Property<string>("Name")
                        .IsRequired()
                        .HasColumnType("nvarchar(max)");

                    b.HasKey("PersonId");

                    b.ToTable("Persons");

                    b.HasData(
                        new
                        {
                            PersonId = 1,
                            Name = "Ayşe"
                        },
                        new
                        {
                            PersonId = 2,
                            Name = "Hilmi"
                        },
                        new
                        {
                            PersonId = 3,
                            Name = "Raziye"
                        },
                        new
                        {
                            PersonId = 4,
                            Name = "Süleyman"
                        },
                        new
                        {
                            PersonId = 5,
                            Name = "Fadime"
                        },
                        new
                        {
                            PersonId = 6,
                            Name = "Şuayip"
                        },
                        new
                        {
                            PersonId = 7,
                            Name = "geovindu"
                        },
                        new
                        {
                            PersonId = 8,
                            Name = "Geovin Du"
                        },
                        new
                        {
                            PersonId = 9,
                            Name = "涂聚文"
                        },
                        new
                        {
                            PersonId = 10,
                            Name = "Muaviye"
                        });
                });

            modelBuilder.Entity("PersonOrderCount", b =>
                {
                    b.Property<int>("Count")
                        .HasColumnType("int");

                    b.Property<string>("Name")
                        .IsRequired()
                        .HasColumnType("nvarchar(max)");

                    b.ToTable((string)null);

                    b.ToView("vw_PersonOrderCount", (string)null);
                });

            modelBuilder.Entity("Order", b =>
                {
                    b.HasOne("Person", "Person")
                        .WithMany("Orders")
                        .HasForeignKey("PersonId")
                        .OnDelete(DeleteBehavior.Cascade)
                        .IsRequired();

                    b.Navigation("Person");
                });

            modelBuilder.Entity("Person", b =>
                {
                    b.Navigation("Orders");
                });
#pragma warning restore 612, 618
        }
    }

  

调用:

//创建表和初始化数据
ApplicationDbContext context = new();

await context.Database.MigrateAsync();


var datas = await context.PersonOrderCounts.ToListAsync();
foreach(var data in datas)
{
    Console.WriteLine(data.Name+"\t\n");
}
Console.WriteLine();

  

 输出:

 

标签:OrderId,donet,Description,Core,PersonId,create,...,new,Price
From: https://www.cnblogs.com/geovindu/p/17092225.html

相关文章

  • netcore 部署问题集
    一、IIS    ①、x86部署问题1、错误信息(附截图):Couldnotfind'aspnetcorev2_inprocess.dll'.Exceptionmessage:2、错误信息(附截图):Failedtostartapplic......
  • OpenCore开机音频设置教程!
    原文来源于黑果魏叔官网,转载需注明出处。前言自从​​OpenCore​​引导版本更新至0.7.7以后,黑苹果开机声音的设置已修改为UEFI音频增益的方式来执行,以下是简单的设置教程。......
  • Linux之LVM管理 pvcreate,vgcreate,lvcreate命令
    一、逻辑卷管理(LVM)概念逻辑卷和逻辑卷管理有助于更加轻松地管理磁盘空间。如果托管逻辑卷的文件系统需要更多空间可以将其卷组中的可用空间分配给逻辑卷,并且可以调整文件......
  • CSharp: donet 6 with Entity Framework Core 6
     ///<summary>//////Entity///</summary>publicclassProduct{publicProduct(){}publicintId{get;......
  • Quartz.Net源码Example之Quartz.Examples.AspNetCore
    Quartz.Examples.AspNetCore​ .NetCore的Web系统,后台主要执行多个触发器任务,前台展示所有触发器信息和正在执行的作业的相关信息,还可以通过访问health-UI来查看目前系统......
  • AspNet Core两种托管模型
    AspNetCore两种托管模型ASPNETCore运行机制......
  • EMR修改Core节点EBS卷大小
    本文用于修改已创建好的EMR集群Core节点EBS磁盘容量,由于业务场景、成本等多方面考虑,需要周期性的调整Core节点的磁盘容量,但是通过人工调整EBS卷大小非常耗时,本文通过Bootstr......
  • 第16章 发布和部署应用程序(ASP.NET Core in Action, 2nd Edition)
    本章包括发布ASP.NETCore应用程序在IIS中托管ASP.NETCore应用程序自定义ASP.NETCore应用程序的URL通过捆绑和缩小优化客户端资产到目前为止,我们在这本书中涵盖了......
  • .NET Core 3.1 通过 Web Service 读写 Salesforce 数据
    1.下载WSDL文件.   2.项目右键->添加->服务引用->WCFWebService,选择刚才下载的WSDL文件,修改命名空间为Salesforce.Core,最后勾选生成同步操作......
  • react 中使用redux的createStore 创建公用变量
    import{createStore}from'redux'exportconststore=createStore(addTodo,0);functionaddTodo(state=0,{type,num=1}){switch(type){......