c code first mysql_.NET Core Mysql EF Core中通过dotnet命令Code First创建生成数据库

1、为此示例创建一个控制台应用程序

1) 使用.NET Core命令行界面(CLI)初始化有效的.NET Core项目和控制台应用程序,然后切换到新创建的文件夹(mysqlefcore)dotnet new console –o mysqlefcore

cd mysqlefcore

2) MySql.Data.EntityFrameworkCore 使用CLI 将包添加到应用程序dotnet add package MySql.Data.EntityFrameworkCore --version 6.10.8

或者,您可以使用Visual Studio中的程序包管理器控制台添加程序包:Install-Package MySql.Data.EntityFrameworkCore -Version 6.10.8

注意版本(例如6.10.8)必须与您使用的实际Connector / NET版本匹配。有关当前版本信息,请参阅表9.2“支持的Entity Framework Core版本”。

3) 恢复项目文件中指定的依赖项和项目特定工具dotnet restore

2、创建模型并运行应用程序

控制器应用程序将使用此EF Core示例中的模型。它由两个与书库相关的实体组成,这些实体将在 LibraryContext类(或数据库上下文)中配置。

1) 创建一个名为的新文件LibraryModel.cs ,然后将以下Book和 Publisher类添加到 mysqlefcore命名空间namespace mysqlefcore

{

public class Book

{

public string ISBN { get; set; }

public string Title { get; set; }

public string Author { get; set; }

public string Language { get; set; }

public int Pages { get; set; }

public virtual Publisher Publisher { get; set; }

}

public class Publisher

{

public int ID { get; set; }

public string Name { get; set; }

public virtual ICollection Books { get; set; }

}

}

2) 创建一个名为的新文件LibraryContext.cs并添加后面的代码。将通用连接字符串替换为适合MySQL服务器配置的字符串

该LibraryContext class 实体使用,它使模型的特定属性,如Key,所需的列,引用等的配置。using Microsoft.EntityFrameworkCore;

using MySQL.Data.EntityFrameworkCore.Extensions;

namespace mysqlefcore

{

public class LibraryContext : DbContext

{

public DbSet Book { get; set; }

public DbSet Publisher { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

{

optionsBuilder.UseMySQL("server=localhost;database=library;user=user;password=password");

}

protected override void OnModelCreating(ModelBuilder modelBuilder)

{

base.OnModelCreating(modelBuilder);

modelBuilder.Entity(entity =>

{

entity.HasKey(e => e.ID);

entity.Property(e => e.Name).IsRequired();

});

modelBuilder.Entity(entity =>

{

entity.HasKey(e => e.ISBN);

entity.Property(e => e.Title).IsRequired();

entity.HasOne(d => d.Publisher)

.WithMany(p => p.Books);

});

}

}

}

3) 将以下代码插入现有 Program.cs文件,替换默认的C#代码using Microsoft.EntityFrameworkCore;

using System;

using System.Text;

namespace mysqlefcore

{

class Program

{

static void Main(string[] args)

{

InsertData();

PrintData();

}

private static void InsertData()

{

using(var context = new LibraryContext())

{

// Creates the database if not exists

context.Database.EnsureCreated();

// Adds a publisher

var publisher = new Publisher

{

Name = "Mariner Books"

};

context.Publisher.Add(publisher);

// Adds some books

context.Book.Add(new Book

{

ISBN = "978-0544003415",

Title = "The Lord of the Rings",

Author = "J.R.R. Tolkien",

Language = "English",

Pages = 1216,

Publisher = publisher

});

context.Book.Add(new Book

{

ISBN = "978-0547247762",

Title = "The Sealed Letter",

Author = "Emma Donoghue",

Language = "English",

Pages = 416,

Publisher = publisher

});

// Saves changes

context.SaveChanges();

}

}

private static void PrintData()

{

// Gets and prints all books in database

using (var context = new LibraryContext())

{

var books = context.Book

.Include(p => p.Publisher);

foreach(var book in books)

{

var data = new StringBuilder();

data.AppendLine($"ISBN: {book.ISBN}");

data.AppendLine($"Title: {book.Title}");

data.AppendLine($"Publisher: {book.Publisher.Name}");

Console.WriteLine(data.ToString());

}

}

}

}

}

4) 使用以下CLI命令还原依赖项,然后运行该应用程序dotnet restore

dotnet run

运行应用程序的输出:ISBN: 978-0544003415

Title: The Lord of the Rings

Publisher: Mariner Books

ISBN: 978-0547247762

Title: The Sealed Letter

Publisher: Mariner Books

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值