A
A
Alex Fedorov2022-01-07 13:31:49
C++ / C#
Alex Fedorov, 2022-01-07 13:31:49

How to project data into a new table?

There is a first model

public class Case
    {
        public int Id { get; set; }
        public string caseName { get; set; }
        public int caseNumber { get; set; }
        public string caseShortName { get; set; }
        public Price Price { get; set; }
    }

Second model

public class Price
    {
        public int Id { get; set; }
        public double casePrice { get; set; }
        public int CaseId { get; set; }
        public Case Case { get; set; }
    }

and the third model, where the name of the case and its price will be projected

public class PriceName
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public double TotalPrice { get; set; }
    }

Main code

public class ApplicationContext : DbContext
    {
        public DbSet<Case> Cases { get; set; } = null!;
        public DbSet<Price> Prices { get; set; } = null!;
        public DbSet<PriceName> PriceNames { get; set; } = null!;
        public ApplicationContext()
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseNpgsql("Server = localhost; Port = 5433; Database = test; Username = postgres; Password = root");
        }

    }

class Program
    {
        static void Main(string[] args)
        {
            using(ApplicationContext db = new ApplicationContext())
            {
                db.Database.EnsureDeleted();
                db.Database.EnsureCreated();
                Case riptide = new Case { caseName = "Operation Riptide Case",  caseNumber = 176264317, caseShortName = "riptide"};
                Case glove = new Case { caseName = "Gloves Case", caseNumber = 175854202, caseShortName = "gloves" };
                Case spectrum = new Case { caseName = "Spectrum Case", caseNumber = 175880240, caseShortName = "spectrum" };
                Case prisma = new Case { caseName = "Prisma Case", caseNumber = 176042493, caseShortName = "prisma" };
                Case fracture = new Case { caseName = "Fracture Case", caseNumber = 176185874, caseShortName = "fracture" };
                Case gamma = new Case { caseName = "Gamma Case", caseNumber = 156110183, caseShortName = "gamma" };
                Case bloodhound = new Case { caseName = "BloodHound Case", caseNumber = 49359031, caseShortName = "bloodhound" };
                Case revolver = new Case { caseName = "Revolver Case", caseNumber = 84444464, caseShortName = "revolver" };


                db.Cases.AddRange(riptide, glove, spectrum, prisma, fracture, gamma, bloodhound, revolver);
                db.SaveChanges();

                Price riptidePrice = new Price { casePrice = 0.58, Case = riptide };
                Price glovePrice = new Price { casePrice = 1.84, Case = glove };
                Price spectrumPrice = new Price { casePrice = 0.61, Case = spectrum };
                Price prismaPrice = new Price { casePrice = 0.07, Case = prisma };
                Price fracturePrice = new Price { casePrice = 0.07, Case = fracture };
                Price gammaPrice = new Price { casePrice = 0.35, Case = gamma };
                Price bloodhoundPrice = new Price { casePrice = 0.50, Case = bloodhound };
                Price revolverPrice = new Price { casePrice = 0.12, Case = revolver };

                db.Prices.AddRange(riptidePrice, glovePrice, spectrumPrice, prismaPrice, fracturePrice, gammaPrice, bloodhoundPrice, revolverPrice);
                db.SaveChanges();
            }

          
        }
    }

and the second applicationcontext where the data will be projected, here comes a request from a user like give the price of a case called riptide. and this result should appear in the database in a table

using(ApplicationContext db = new ApplicationContext())
            {
                var report = db.Cases.Where(x => x.caseShortName == "riptide").Select(s => new PriceName
                {
                    Name = s.caseName,
                    TotalPrice = s.Price.casePrice
                });
            }

but my data does not appear in the pricename table and the connection is busy error occurs.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question