C# 更新数据库仅在mysql数据库中使用.net core创建_efmigrationtable?
问题: 我对.net核心非常陌生。所以这里我尝试的是将我的.NETCoreAPI连接到我的MySQL数据库。为此,我创建了这样一个数据模型类C# 更新数据库仅在mysql数据库中使用.net core创建_efmigrationtable?,c#,mysql,asp.net-core,C#,Mysql,Asp.net Core,问题: 我对.net核心非常陌生。所以这里我尝试的是将我的.NETCoreAPI连接到我的MySQL数据库。为此,我创建了这样一个数据模型类 using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Th
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
namespace webApi.Models
{
public class PaymentDetail
{
[Key]
public int PMID { get; set; }
[Required]
[Column(TypeName ="varchar(100)")]
public string CardOwnerName { get; set; }
[Column(TypeName = "varchar(16)")]
[Required]
public string CardNumber { get; set; }
[Required]
[Column(TypeName = "varchar(5)")]
public string ExpirationDate { get; set; }
[Required]
[Column(TypeName = "varchar(3)")]
public string CVV { get; set; }
}
}
然后将Db上下文类设置为如下所示
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace webApi.Models
{
public class PaymentDetailContext:DbContext
{
public PaymentDetailContext(DbContextOptions<PaymentDetailContext> options) : base(options)
{
}
public DbSet<PaymentDetail> PaymentDetails { get; set; }
}
}
services.AddDbContextPool<PaymentDetailContext>(options =>
options.UseMySql(Configuration.GetConnectionString("DevConnection")));
使用Microsoft.EntityFrameworkCore;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
名称空间webApi.Models
{
公共类PaymentDetailContext:DbContext
{
public PaymentDetailContext(DbContextOptions选项):基本(选项)
{
}
公共DbSet PaymentDetails{get;set;}
}
}
之后,我创建了这个连接字符串,并在startup.cs类中使用它
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace webApi.Models
{
public class PaymentDetailContext:DbContext
{
public PaymentDetailContext(DbContextOptions<PaymentDetailContext> options) : base(options)
{
}
public DbSet<PaymentDetail> PaymentDetails { get; set; }
}
}
services.AddDbContextPool<PaymentDetailContext>(options =>
options.UseMySql(Configuration.GetConnectionString("DevConnection")));
services.AddDbContextPool(选项=>
options.UseMySql(Configuration.GetConnectionString(“DevConnection”));
成功运行添加迁移命令后。运行updatedatabase命令时,它只创建数据库和_efmigarion表,而不创建我的付款明细表。有人能帮我解决这个问题吗?我试图找出这个问题,但我做不到。由于我对这个.net核心还不熟悉,如果有人能帮我解决这个问题,我真的很感激。谢谢请检查迁移文件,将有两种方法称为up()和down() up()方法应该为PaymentDetail创建表迁移代码
down()。 更改paymentdetailcontext类构造函数,如下所示
public PaymentDetailContext(DbContextOptions options) : base(options)
{
}
在statup.cs中,还将configure service方法中的代码更改为以下内容
services.AddDbContext<PaymentDetailContext>(item => item.UseSqlServer(Configuration.GetConnectionString("DevConnection")));
services.AddDbContext(item=>item.UseSqlServer(Configuration.GetConnectionString(“DevConnection”));
看看它是否解决了您的问题然后迁移就可以了,在启动类上添加services.AddDbContext而不是services.AddDbContextPool