returning data from database

SaeedP

Well-known member
Joined
Oct 21, 2020
Messages
97
Programming Experience
3-5
Hello,

By considering this code:

C#:
[HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }

If I don't want to insert data here and return what is in the model what changes should I do?

thanks,
 
You are not inserting data. You are randomly creating new data.

The quick, but obtuse way is to Lazy<T> to randomly generate your forecast data once and then after that only the lazily created data.

The traditional way is to randomly generate your forecast data in the constructor, and then from that point on only read the that generated data.
 
You are not inserting data. You are randomly creating new data.

The quick, but obtuse way is to Lazy<T> to randomly generate your forecast data once and then after that only the lazily created data.

The traditional way is to randomly generate your forecast data in the constructor, and then from that point on only read the that generated data.

Yes, but If I need to return what is in the database what should I do?

Something like this:

C#:
return View(_context.Employees.ToList());

In ASP.net Core.
 
How are you reading from the database?

If you are using EF (which I don't recommend), you can do like you did with the view above.
 
How are you reading from the database?

If you are using EF (which I don't recommend), you can do like you did with the view above.
I just want to return data in my react view in a table. You can see react code here:

C#:
import React, { Component } from 'react';

export class Grid1 extends Component {
    static displayName = FetchData.name;

    constructor(props) {
        super(props);
        this.state = { forecasts: [], loading: true };
    }

    componentDidMount() {
        this.populateWeatherData();
    }

    static renderForecastsTable(forecasts) {
        return (
            <table className='table table-striped' aria-labelledby="tabelLabel">
                <thead>
                    <tr>
                        <th>Id</th>
                        <th>FileName (C)</th>
                        <th>FormFile (F)</th>
                        
                    </tr>
                </thead>
                <tbody>
                    {forecasts.map(forecast =>
                        <tr key={forecast.Id}>
                            <td>{forecast.FileName}</td>
                            <td>{forecast.FormFile}</td>
                            
                        </tr>
                    )}
                </tbody>
            </table>
        );
    }

    render() {
        let contents = this.state.loading
            ? <p><em>Loading...</em></p>
            : FetchData.renderForecastsTable(this.state.forecasts);

        return (
            <div>
                <h1 id="tabelLabel" >Grid1</h1>
                <p>This component demonstrates fetching data from the server.</p>
                {contents}
            </div>
        );
    }

    async populateWeatherData() {
        const response = await fetch('FileModel');
        const data = await response.json();
        this.setState({ forecasts: data, loading: false });
    }
}

If I want to use Entity framework core what code should I write ( assume not using dbContext).

If not use Entity framework core, which way you suggest?
 
I would recommend hand written Repository pattern implementation with an ADO.NET backend. But if that becomes too repetitive, consider using Dapper or AutoMapper.
 
Is not possible by using the entity framework Core to do this task?
Unless we use dbContext?
Of course, I will read about Auto Mapper.

thnx
 
Yes, it is possible using Entity Framework. I just personally don't recommend it because I'd been burned too many times in the past with bugs is the older versions of EF. People say that the current versions are better. I've just not bothered investing anymore time into it. A lot of the contemporary tutorials and learning materials presume the use of Entity Framework, so you might as well use it.
 
Back
Top Bottom