Windows forms App

mehdi selk 1 Reputation point
2021-12-15T22:37:28.223+00:00

Im am a begginer in c# and i am using visual studio 2022
i find a problem when i use windows forms using entity famework.
if i use windows forms app (.NET Framework) i find this problem
158023-screenshot-2021-12-15-232116.png

and when i use windows forms app i find this problem of c# language version

157990-screenshot-2021-12-15-232447.png

i downloaded entity framework

158061-screenshot-2021-12-15-232840.png

in both of windows forms app types

158018-screenshot-2021-12-15-232322.png

i need help please

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,903 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,921 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,012 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Jaliya Udagedara 2,821 Reputation points MVP
    2021-12-15T23:26:15.713+00:00

    From the Error that is shown under Error List, can you add this: <LangVersion>latest</LangVersion> under <PropertyGroup> please. Something like below,

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net6.0-windows</TargetFramework>
        <Nullable>enable</Nullable>
        <UseWindowsForms>true</UseWindowsForms>
        <ImplicitUsings>enable</ImplicitUsings>
        <LangVersion>latest</LangVersion>
      </PropertyGroup>
    
    </Project>
    

  2. Jack J Jun 24,516 Reputation points Microsoft Vendor
    2021-12-16T07:49:39.957+00:00

    @mehdi selk , Based on your picture, I find that you are using .net core winform. Therefore, we need to use EF core to get data from database instead of Entityframework.

    Please refer to the following steps to use ef-core in .net core winform app.

    First, Please install the following nuget-package: (Please choose other type of ef core If you access other database)

    158117-image.png

    Second, Please create model class and dbcontext class.

    public class Student  
    {  
        [Key]  
        public int Id { get; set; }   
      
        public string Name { get; set; }          
      
        public DateTime Date { get; set; }  
    }  
      
      
    public class MyDbContext:DbContext  
    {  
        public DbSet<Student> Students { get; set; }  
      
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)  
        {  
            optionsBuilder.UseSqlServer(@"connstr");  
        }  
             
    }  
    

    Third, Please execute the following command in your Package Manager Console window.

     Add-Migration First  
      
     Update-Database  
    

    Finally, you could try the following code to insert data to database and show the data in datagridview.

    private void Form1_Load(object sender, EventArgs e)  
            {  
                using (MyDbContext dbContext = new MyDbContext())  
                {  
                    dbContext.Students.Add(new Student { Id=1001, Date=DateTime.Now, Name="test1" });  
                    dbContext.Students.Add(new Student { Id=1002, Date=DateTime.Now, Name="test2" });  
                    dbContext.Students.Add(new Student { Id=1003, Date=DateTime.Now, Name="test3" });  
                    dbContext.SaveChanges();  
                }  
            }  
            MyDbContext dbContext = new MyDbContext();  
            private void button1_Click(object sender, EventArgs e)  
            {  
                var result = from c in dbContext.Students  
                             select c;  
                dataGridView1.DataSource=result.ToList();  
            }  
      
            private void button2_Click(object sender, EventArgs e)  
            {  
                var result = from c in dbContext.Students  
                             select new  
                             {  
                                 c.Name,  
                                 c.Date  
                             };  
                dataGridView1.DataSource= result.ToList();  
            }  
    

    Result:

    158110-2.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Karen Payne MVP 35,436 Reputation points
    2021-12-27T01:08:18.347+00:00

    I have a decent Entity Framework 6 code sample here done a while ago (under windows 7 but will run under windows 10) using SQL-Server. Before using you must open NuGet package manager and perform a restore.

    160420-grid.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.