我最近开始学习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."
我在64位CPU上有64位Windows 10,但我不明白如何以另一位速率发布项目?
好的,这听起来很荒谬,但是我所需要的只是更新NuGet包Microsoft.Data.SqlClient
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句