returning data from database

SaeedP

Active member
Joined
Oct 21, 2020
Messages
34
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,
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,416
Location
Chesapeake, VA
Programming Experience
10+
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.
 

SaeedP

Active member
Joined
Oct 21, 2020
Messages
34
Programming Experience
3-5
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.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,416
Location
Chesapeake, VA
Programming Experience
10+
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.
 

SaeedP

Active member
Joined
Oct 21, 2020
Messages
34
Programming Experience
3-5
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?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,416
Location
Chesapeake, VA
Programming Experience
10+
I would recommend hand written Repository pattern implementation with an ADO.NET backend. But if that becomes too repetitive, consider using Dapper or AutoMapper.
 

SaeedP

Active member
Joined
Oct 21, 2020
Messages
34
Programming Experience
3-5
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
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,416
Location
Chesapeake, VA
Programming Experience
10+
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.
 
Top Bottom