仙骷,像中枪一样 歌词,金庸群侠传3无敌版
目录
entity framework (ef) core 是轻量化、可扩展、开源和跨平台版的常用 entity framework 数据访问技术。
ef core 可用作对象关系映射程序 (o/rm),以便于 .net 开发人员能够使用 .net 对象来处理数据库,这样就不必经常编写大部分数据访问代码了。
ef core 支持多个数据库引擎,请参阅了解详细信息。
在本教程中,将创建一个 .net core 控制台应用,该应用使用 entity framework core 对 microsoft sql server 数据库执行数据访问。
新建控制台项目:
执行 powershell 命令
dotnet new console -o consoleapp
如下图
将当前目录更改为应用程序的目录,如下所示:
cd consoleapp
安装 microsoft.entityframeworkcore.sqlserver 和 microsoft.entityframeworkcore.tools
dotnet add package microsoft.entityframeworkcore.sqlserver dotnet add package microsoft.entityframeworkcore.tools
运行 dotnet restore
来安装新的程序包。
使用以下内容创建一个新的 model.cs 文件。
using microsoft.entityframeworkcore; using system.collections.generic; namespace consoleapp { public class bloggingcontext : dbcontext { public dbset<blog> blogs { get; set; } public dbset<post> posts { get; set; } protected override void onconfiguring(dbcontextoptionsbuilder optionsbuilder) { optionsbuilder.usesqlserver("server=你的数据库地址;database=consoleapp;user id=你的数据库账号;password=你的数据库密码;"); } } public class blog { public int blogid { get; set; } public string url { get; set; } public icollection<post> posts { get; set; } } public class post { public int postid { get; set; } public string title { get; set; } public string content { get; set; } public int blogid { get; set; } public blog blog { get; set; } } }
在实际应用程序中,应将每个类放在单独的文件中,并将连接字符串放在配置文件或环境变量中。 为简化本教程,所有内容均放在一个文件中。
有了模型后,即可通过迁移创建数据库。
dotnet ef migrations add initialcreate
以为迁移搭建基架,并为模型创建一组初始表。dotnet ef database update
以将新迁移应用到数据库。 在应用迁移之前,此命令可创建数据库。打开 program.cs 并将内容替换为以下代码:
using system; namespace consoleapp { class program { static void main(string[] args) { using (var db = new bloggingcontext()) { db.blogs.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.blogs) { console.writeline(" - {0}", blog.url); } } } } }
运行 dotnet run
从控制台测试应用。
在本教程中,将使用 entity framework core 构建执行基本数据访问的 asp.net core mvc 应用程序。
单击“确定”
对于本教程,无需安装提供程序包,因为本教程使用 sql server。 sql server 提供程序包包含在 microsoft.aspnetcore.app 元包中。
将此文件的内容替换为以下代码:
using system.collections.generic; using microsoft.entityframeworkcore; namespace webapplication.models { public class bloggingcontext : dbcontext { public bloggingcontext(dbcontextoptions<bloggingcontext> options) : base(options) { } public dbset<blog> blogs { get; set; } public dbset<post> posts { get; set; } } public class blog { public int blogid { get; set; } public string url { get; set; } public icollection<post> posts { get; set; } } public class post { public int postid { get; set; } public string title { get; set; } public string content { get; set; } public int blogid { get; set; } public blog blog { get; set; } } }
生产应用通常会将每个类放在单独的文件中。 为简单起见,本教程将这些类放在一个文件中。
若要使 bloggingcontext
可用于 mvc 控制器,请在 startup.cs
中将其注册为服务。
在应用程序启动过程中,通过依赖关系注入 注册服务(如 bloggingcontext),以便能够通过构造函数的参数和属性向使用服务的组件(如 mvc 控制器)自动提供该服务。
在 startup.cs 中,添加以下 using 语句:
using webapplication.models; using microsoft.entityframeworkcore;
将以下 手动高亮
的代码添加到 configureservices
方法:
public void configureservices(iservicecollection services) { services.configure<cookiepolicyoptions>(options => { // this lambda determines whether user consent for non-essential cookies is needed for a given request. options.checkconsentneeded = context => true; options.minimumsamesitepolicy = samesitemode.none; }); services.addmvc().setcompatibilityversion(compatibilityversion.version_2_1); var connection = @"server=你的数据库地址;database=webapplication;user id=你的数据库账号;password=你的数据库密码;"; // 手动高亮 services.adddbcontext<bloggingcontext> // 手动高亮 (options => options.usesqlserver(connection)); // 手动高亮 }
生产应用通常会将连接字符串放在配置文件或环境变量中。 为简单起见,本教程在代码中定义它。
以下步骤使用迁移创建数据库。
运行以下命令:
add-migration initialcreate update-database
如果收到错误,指出 the term 'add-migration' is not recognized as the name of a cmdlet,请关闭并重新打开 visual studio。
add-migration 命令为迁移搭建基架,以便为模型创建一组初始表。 update-database 命令创建数据库并向其应用新的迁移。
生成 blog 实体控制器和视图的基架。
单击 添加。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Net Core Web Api项目与在NginX下发布的方法
asp.net core3.1 引用的元包dll版本兼容性问题解决方案
IdentityServer4实现.Net Core API接口权限认证(快速入门)
ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现
网友评论