question

mehdiselk-5749 avatar image
0 Votes"
mehdiselk-5749 asked karenpayneoregon answered

Windows forms App

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


dotnet-csharpwindows-formsdotnet-entity-framework
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Can you share the content of .csproj file please

0 Votes 0 ·

I am sorry, not that.

157949-image.png

0 Votes 0 ·
image.png (121.8 KiB)
Show more comments
JaliyaUdagedara avatar image
0 Votes"
JaliyaUdagedara answered JaliyaUdagedara commented

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>
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

its not working

0 Votes 0 ·

if u have any way to talk to me
maybe discord or something so i can show you the problem

i really need help with this

0 Votes 0 ·

Sure, I am on Teams, jaliya.udagedara@hotmail.com

0 Votes 0 ·
JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered JackJJun-MSFT commented

@mehdiselk-5749 , 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.


image.png (21.3 KiB)
2.png (16.1 KiB)
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

its acctually not working
if you have any way to talk privatly
maybe teams or discord ?

0 Votes 0 ·

@mehdiselk-5749, Sorry for that, we only provide forum support. What is your problem now? Did you still meet the error or other errors?

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

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



grid.png (63.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.