System.TypeInitializationException возникает - PullRequest
0 голосов
/ 11 июля 2020

Я недавно начал изучать Entity Framework, и я новичок. Я написал первую простую программу, но она не работает. Я пытался найти решение в Google, но безрезультатно. Надеюсь найти здесь ответ.

ApplicationContext.cs


using Microsoft.EntityFrameworkCore;

namespace HelloApp
{
    class ApplicationContext: DbContext     
    {
        public DbSet<User> Users { get; set; }     

        public ApplicationContext()
        {
            Database.EnsureCreated();
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)   
        {
            optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=helloappdb;Trusted_Connection=True;");
        }
    }
}


Program.cs

using System;
using System.Linq;

namespace HelloApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ApplicationContext db = new ApplicationContext())
            {
                User user1 = new User { Name = "Tom", Age = 33 };
                User user2 = new User { Name = "Alice", Age = 26 };

                db.Users.Add(user1);
                db.Users.Add(user2);
                db.SaveChanges();
                Console.WriteLine("Successfully saved!");

                var users = db.Users.ToList();
                Console.WriteLine("List of objects:");
                foreach (User u in users)
                {
                    Console.WriteLine($"{u.Id}.{u.Name} - {u.Age}");
                }
            }
        }

    }
}

Спасибо за вашу помощь !!!

[Обновление] Я понял, что System.TypeInitializationException разъясняет мне, в чем проблема:

The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception. ---> System.ComponentModel.Win32Exception: Failed to load C:\Users\Босс\Desktop\x86\SNI.dll 

Я искал в Google, но смог найти только одно возможное объяснение:

"That makes me suspect that you build it on a 64 bit system and then just transferred the files to the other machine. You need to publish the project for an x86 target to get the right native dependency resolved. Give it a try."

I у вас 64-битный Windows 10 на 64-битном ЦП, и я не могу понять, как опубликовать sh мой проект с другим битрейтом?

1 Ответ

0 голосов
/ 12 июля 2020

Хорошо, это может прозвучать смешно, но все, что мне нужно, это обновление пакета NuGet Microsoft.Data.SqlClient

...