怎样建设个人手机网站,网站更换域名seo,大型建设网站制作,九斗云网络推广营销数据目录
一、创建.NET Framework 4.8控制台应用
二、建立数据库
1. 在SSMS中建立数据库Blogging
2.在VS上新建数据库连接
三、安装EF程序包
四、自动生成EF模型和上下文
1.Blog.cs类的模型
2.Post.cs类的模型
3.BloggingContext.cs数据库上下文
五、编写应用程序吧 我们…目录
一、创建.NET Framework 4.8控制台应用
二、建立数据库
1. 在SSMS中建立数据库Blogging
2.在VS上新建数据库连接
三、安装EF程序包
四、自动生成EF模型和上下文
1.Blog.cs类的模型
2.Post.cs类的模型
3.BloggingContext.cs数据库上下文
五、编写应用程序吧 我们都知道.NET Framework最后一个更新版本是4.8.1而曾经支持.NET Framework的EF版本却一直更新到现在仍然在不断创新。当前主流的VS2022仍然支持.NET Framework 4.8在.NET Framework 4.8下使用EF访问数据库不经过一番额外的操作想直接使用VS2022的默认安装是不可能行得通的。幸好VS2022没有关闭在.NET Framework 4.8下使用EF访问数据库的大门。作者经过学习、整理成本文发布出来提供给有需要的人。 本文的核心内容是在VS2022中给.NET Framework 4.8找到并安装恰当的、支持的EF版本。只有合适的EF版本才能支持.NET Framework 4.8通过EF访问数据库。
一、创建.NET Framework 4.8控制台应用 如何创建此处略去。
二、建立数据库
1. 在SSMS中建立数据库Blogging 新建数据库新建查询粘贴如下数据库源码执行。
CREATE DATABASE [Blogging];
GOUSE [Blogging];
GOCREATE TABLE [Blog] ([BlogId] int NOT NULL IDENTITY,[Url] nvarchar(max) NOT NULL,CONSTRAINT [PK_Blog] PRIMARY KEY ([BlogId])
);
GOCREATE TABLE [Post] ([PostId] int NOT NULL IDENTITY,[BlogId] int NOT NULL,[Content] nvarchar(max),[Title] nvarchar(max),CONSTRAINT [PK_Post] PRIMARY KEY ([PostId]),CONSTRAINT [FK_Post_Blog_BlogId] FOREIGN KEY ([BlogId]) REFERENCES [Blog] ([BlogId]) ON DELETE CASCADE
);
GOINSERT INTO [Blog] (Url) VALUES
(http://blogs.msdn.com/dotnet),
(http://blogs.msdn.com/webdev),
(http://blogs.msdn.com/visualstudio)
GO
2.在VS上新建数据库连接 在上述新建项目中新建数据库连接连接数据库Blogging。如何建立此连接此处略去。
Data SourceDESKTOP-3LV13FS;Initial CatalogBlogging;Integrated SecurityTrue
三、安装EF程序包 找到适合.NET Framework 4.8版本的程序包并安装是本文方法得以成立关键点。经过作者一番测试和查阅资料最适合的EF版本是 3.1.32比这再高的版本都不支持.NET Framework了。应该安装的程序包
NuGet Gallery | Microsoft.EntityFrameworkCore 3.1.32 NuGet\Install-Package Microsoft.EntityFrameworkCore -Version 3.1.32 NuGet Gallery | Microsoft.EntityFrameworkCore.SqlServer 3.1.32 NuGet\Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.32 NuGet Gallery | Microsoft.EntityFrameworkCore.Design 3.1.32 NuGet\Install-Package Microsoft.EntityFrameworkCore.Design -Version 3.1.32
NuGet Gallery | Microsoft.EntityFrameworkCore.Tools 3.1.32 NuGet\Install-Package Microsoft.EntityFrameworkCore.Tools -Version 3.1.32 安装方法VS2022上述项目页窗体→工具→NuGet包管理器→程序包管理器控制台→把上面蓝色的文字复制粘贴到控制台的PM后面回车等待。观察右侧资源管理器增加了一片片的引用。 安装成功后程序包管理器控制台没有红色警告提示信息并有提示安装成功。也可以检查App.config查看是否安装成功。
//App.config
?xml version1.0 encodingutf-8?
configurationstartup supportedRuntime versionv4.0 sku.NETFramework,Versionv4.8 //startupruntimeassemblyBinding xmlnsurn:schemas-microsoft-com:asm.v1dependentAssemblyassemblyIdentity nameSystem.Runtime.CompilerServices.Unsafe publicKeyTokenb03f5f7f11d50a3a cultureneutral /bindingRedirect oldVersion0.0.0.0-4.0.6.0 newVersion4.0.6.0 //dependentAssemblydependentAssemblyassemblyIdentity nameSystem.Memory publicKeyTokencc7b13ffcd2ddd51 cultureneutral /bindingRedirect oldVersion0.0.0.0-4.0.1.1 newVersion4.0.1.1 //dependentAssemblydependentAssemblyassemblyIdentity nameMicrosoft.Extensions.Primitives publicKeyTokenadb9793829ddae60 cultureneutral /bindingRedirect oldVersion0.0.0.0-3.1.32.0 newVersion3.1.32.0 //dependentAssemblydependentAssemblyassemblyIdentity nameMicrosoft.Extensions.Configuration.Abstractions publicKeyTokenadb9793829ddae60 cultureneutral /bindingRedirect oldVersion0.0.0.0-3.1.32.0 newVersion3.1.32.0 //dependentAssemblydependentAssemblyassemblyIdentity nameMicrosoft.Extensions.DependencyInjection.Abstractions publicKeyTokenadb9793829ddae60 cultureneutral /bindingRedirect oldVersion0.0.0.0-3.1.32.0 newVersion3.1.32.0 //dependentAssemblydependentAssemblyassemblyIdentity nameMicrosoft.Extensions.Caching.Abstractions publicKeyTokenadb9793829ddae60 cultureneutral /bindingRedirect oldVersion0.0.0.0-3.1.32.0 newVersion3.1.32.0 //dependentAssemblydependentAssemblyassemblyIdentity nameMicrosoft.Extensions.Options publicKeyTokenadb9793829ddae60 cultureneutral /bindingRedirect oldVersion0.0.0.0-3.1.32.0 newVersion3.1.32.0 //dependentAssemblydependentAssemblyassemblyIdentity nameMicrosoft.Extensions.Logging.Abstractions publicKeyTokenadb9793829ddae60 cultureneutral /bindingRedirect oldVersion0.0.0.0-3.1.32.0 newVersion3.1.32.0 //dependentAssemblydependentAssemblyassemblyIdentity nameSystem.Threading.Tasks.Extensions publicKeyTokencc7b13ffcd2ddd51 cultureneutral /bindingRedirect oldVersion0.0.0.0-4.2.0.1 newVersion4.2.0.1 //dependentAssemblydependentAssemblyassemblyIdentity nameMicrosoft.Extensions.DependencyInjection publicKeyTokenadb9793829ddae60 cultureneutral /bindingRedirect oldVersion0.0.0.0-3.1.32.0 newVersion3.1.32.0 //dependentAssemblydependentAssemblyassemblyIdentity nameSystem.ComponentModel.Annotations publicKeyTokenb03f5f7f11d50a3a cultureneutral /bindingRedirect oldVersion0.0.0.0-4.2.1.0 newVersion4.2.1.0 //dependentAssembly/assemblyBinding/runtime
/configuration
四、自动生成EF模型和上下文 用于访问已有数据库的EF模型和上下文是可以通过编程而自动生成的。
PM Scaffold-DbContext ServerDESKTOP-3LV13FS;DatabaseBlogging;Trusted_ConnectionTrue; Microsoft.EntityFrameworkCore.SqlServer
Build started...
Build succeeded.
PM 右侧资源管理器自动生成与映射到了数据库的Blog.cs类的模型、Post.cs类的模型数据库有几个列就自动生成几个类的模型和BloggingContext.cs数据库上下文。 1.Blog.cs类的模型
//Blog EF模型
using System;
using System.Collections.Generic;// Code scaffolded by EF Core assumes nullable reference types (NRTs) are not used or disabled.
// If you have enabled NRTs for your project, then un-comment the following line:
// #nullable disablenamespace _10_8
{public partial class Blog{public Blog(){Post new HashSetPost();}public int BlogId { get; set; }public string Url { get; set; }public virtual ICollectionPost Post { get; set; }}
}
2.Post.cs类的模型
//Post EF模型
using System;
using System.Collections.Generic;// Code scaffolded by EF Core assumes nullable reference types (NRTs) are not used or disabled.
// If you have enabled NRTs for your project, then un-comment the following line:
// #nullable disablenamespace _10_8
{public partial class Post{public int PostId { get; set; }public int BlogId { get; set; }public string Content { get; set; }public string Title { get; set; }public virtual Blog Blog { get; set; }}
}
3.BloggingContext.cs数据库上下文
//DbContext类的上下文
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;// Code scaffolded by EF Core assumes nullable reference types (NRTs) are not used or disabled.
// If you have enabled NRTs for your project, then un-comment the following line:
// #nullable disablenamespace _10_8
{public partial class BloggingContext : DbContext{public BloggingContext(){}public BloggingContext(DbContextOptionsBloggingContext options): base(options){}public virtual DbSetBlog Blog { get; set; }public virtual DbSetPost Post { get; set; }protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){if (!optionsBuilder.IsConfigured){
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId723263 for guidance on storing connection strings.optionsBuilder.UseSqlServer(ServerDESKTOP-3LV13FS;DatabaseBlogging;Trusted_ConnectionTrue;);}}protected override void OnModelCreating(ModelBuilder modelBuilder){modelBuilder.EntityBlog(entity {entity.Property(e e.Url).IsRequired();});modelBuilder.EntityPost(entity {entity.HasOne(d d.Blog).WithMany(p p.Post).HasForeignKey(d d.BlogId);});OnModelCreatingPartial(modelBuilder);}partial void OnModelCreatingPartial(ModelBuilder modelBuilder);}
}五、编写应用程序吧 你想让这段程序干什么现在就开始编写属于你的应用吧通过应用程序给Blog里增加一个新的网址并输出到控制台。
//.NET Framework4.8下通过EF给已有数据库增加一条记录
//.NET Framework4.8下通过EF给已有数据库增加一条记录
using System;
using static System.Net.WebRequestMethods;namespace _10_8
{internal class Program{static void Main(string[] args){using (var db new BloggingContext()){db.Blog.Add(new Blog { Url http://blogs.msdn.com/adonet });var count db.SaveChanges();Console.WriteLine({0} records saved to database, count);Console.WriteLine();Console.WriteLine(All blogs in database:);foreach (var blog in db.Blog){Console.WriteLine( - {0}, blog.Url);}}}}
} //运行结果
/*
1 records saved to databaseAll blogs in database:-http://blogs.msdn.com/dotnet-http://blogs.msdn.com/webdev-http://blogs.msdn.com/visualstudio-http://blogs.msdn.com/adonet
请按任意键继续. . .*/