vs mysql搭建,.NET Core1.1+VS2017RC+MySQL+EF搭建多层Web应用程序

.NET Core1.1+VS2017RC+MySQL+EF搭建多层Web应用程序,先贴上解决方案截图

603c1646a0e100a8b29c9548a7845507.png

一、新建4个解决方案文件夹

1-Presentation

2-Application

3-Domain

4-Infrastructure

二、在解决方案文件夹中分别创建项目

b8b5441101b9c374804f9d12494b7c52.png

新建.NET Core Web应用程序,【身份验证】需要选择【个人用户账户】否则无法执行migrations操作,猜想原因可能少了某个NuGet包,具体没去测试验证

如果不选【个人用户账户】,migrations操作时会报【No executable found matching command "dotnet-ef"】错误

79b097ee1770c3b3ccd9b6dca9cbd550.png

其余项目创建省略

项目引用关系:

1.ContosoUniversity.WebAdmin引用ContosoUniversity.Application、ContosoUniversity.Domain

2.ContosoUniversity.Application引用ContosoUniversity.Repository、ContosoUniversity.Domain

3.ContosoUniversity.Repository引用ContosoUniversity.Domain

4.ContosoUniversity.Domain不引用任何项目

三、ContosoUniversity.Domain项目中添加Microsoft.EntityFrameworkCore.Tools1.1.0-preview4-final

NuGet命令:Install-Package Microsoft.EntityFrameworkCore.Tools -Pre

四、ContosoUniversity.Domain项目添加Student、SchoolContext、DbInitializer类

Student:POCO对象,对应数据库中的Student表

SchoolContext:数据库上下文,用于数据库CRUD以及Migrations操作

DbInitializer:初始化数据库并添加测试数据

usingSystem;namespaceContosoUniversity.Domain

{public classStudent

{public int ID { get; set; }public string LastName { get; set; }public string FirstMidName { get; set; }public DateTime EnrollmentDate { get; set; }

}

}

usingMicrosoft.EntityFrameworkCore;namespaceContosoUniversity.Domain.Data

{public classSchoolContext : DbContext

{public SchoolContext(DbContextOptions options) : base(options)

{

}public DbSet Students { get; set; }protected override voidOnModelCreating(ModelBuilder modelBuilder)

{

modelBuilder.Entity().ToTable("Student");

}

}

}

usingSystem;usingSystem.Linq;namespaceContosoUniversity.Domain.Data

{public static classDbInitializer

{public static voidInitialize(SchoolContext context)

{

context.Database.EnsureCreated();//Look for any students.

if(context.Students.Any())

{return; //DB has been seeded

}var students = newStudent[]

{new Student{FirstMidName="Carson",LastName="Alexander",EnrollmentDate=DateTime.Parse("2005-09-01")},new Student{FirstMidName="Meredith",LastName="Alonso",EnrollmentDate=DateTime.Parse("2002-09-01")},new Student{FirstMidName="Arturo",LastName="Anand",EnrollmentDate=DateTime.Parse("2003-09-01")},new Student{FirstMidName="Gytis",LastName="Barzdukas",EnrollmentDate=DateTime.Parse("2002-09-01")},new Student{FirstMidName="Yan",LastName="Li",EnrollmentDate=DateTime.Parse("2002-09-01")},new Student{FirstMidName="Peggy",LastName="Justice",EnrollmentDate=DateTime.Parse("2001-09-01")},new Student{FirstMidName="Laura",LastName="Norman",EnrollmentDate=DateTime.Parse("2003-09-01")},new Student{FirstMidName="Nino",LastName="Olivetto",EnrollmentDate=DateTime.Parse("2005-09-01")}

};foreach (Student s instudents)

{

context.Students.Add(s);

}

context.SaveChanges();

}

}

}

五、ContosoUniversity.WebAdmin项目修改

1.appsetting.json文件添加MySQL连接字符串

"ConnectionStrings": {"DefaultConnection": "server=xxx;user id=xxx;password=xxx;database=ContosoUniversity;"}

2.添加NuGet包MySql.Data 6.10.0-alpha、MySql.Data.EntityFrameworkCore 6.10.0-alpha、Microsoft.EntityFrameworkCore.Tools 1.1.0-preview4-final

MySql版本不要选7.0.6-IR31,项目跑起来会报"MySql.Data.EntityFrameworkCore.Storage.Internal.MySQLCommandBuilderFactory..ctor(ISensitiveDataLogger logger, DiagnosticSource diagnosticSource, IRelationalTypeMapper typeMapper)"错误

47358e2d63be5b8d58129fca60edb865.png

3.StartUp类ConfigureServices方法注入数据库上下文

public voidConfigureServices(IServiceCollection services)

{

services.AddDbContext(options =>options.UseMySQL(Configuration.GetConnectionString("DefaultConnection"), b => b.MigrationsAssembly("ContosoUniversity.WebAdmin")));//Add framework services.

services.AddMvc();

}

注意,标红的代码不可缺少,否则EntityFramework无法执行Migrations,报错信息如下

0a6c38563bc1bc5565a00f09be68d442.png

4.StartUp添加数据库初始化

改造Configure方法签名,添加SchoolContext参数

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, SchoolContext context)

Configure方法末尾添加数据库初始化代码

DbInitializer.Initialize(context);

最后

把其余各层的代码都加上项目就可以跑起来了,通过Migrations操作维护开发库,.NET Core+MySQL+EF使用VS2017RC构建项目的坑基本就是这些了。。

注意

NuGet包Install或Uninstall命名执行后,查看VS2017RC中依赖的NuGet包发现没有变化(实际上已Install或Uninstall,VS2017RC没有刷新),此时需要关闭解决方案重新打开,这时NuGet依赖才会刷新,这时VS2017RC的一个BUG!

s0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我理解了你的问题。关于 Hangfire、MySQL 和 ASP.NET Core 3.1 的使用,我可以给出一些异常解决的建议。 首先,确保你已经按照官方文档正确地安装和配置了 Hangfire 和 MySQL。然后,根据你遇到的异常,尝试以下解决方法: 1. "Could not load file or assembly 'MySql.Data' or one of its dependencies" 异常 这个异常通常是由于缺少 MySQL 数据库驱动程序引起的。你需要在项目添加对 MySQL 数据库驱动程序的引用。可以通过 NuGet 包管理器来安装 MySQL.Data。 2. "Specified key was too long; max key length is 767 bytes" 异常 这个异常是由于 MySQL 的索引长度限制引起的。解决方法是,在你的 DbContext 覆盖 OnModelCreating 方法,将所有字符串属性的最大长度设置为 255。 ``` protected override void OnModelCreating(ModelBuilder modelBuilder) { foreach (var property in modelBuilder.Model.GetEntityTypes() .SelectMany(t => t.GetProperties()) .Where(p => p.ClrType == typeof(string))) { property.SetMaxLength(255); } } ``` 3. "MySqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding" 异常 这个异常通常是由于 MySQL 连接超时引起的。解决方法是,在连接字符串添加 Connection Timeout 参数,例如: ``` "Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;Connection Timeout=60;" ``` 这将使连接超时时间为 60 秒。 希望这些解决方法能帮助你解决异常问题。如果你还有其他问题,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值