Dotnet core+MVC+Code First 数据迁移

 

几天前在学习efcore code first时在迁移方面遇到了难题,经过一段时间的不断尝试,最终解决了,因此再次记录一下解决的办法,以方便记忆.(具体遇到的问题:https://ask.csdn.net/questions/703937)

首先是学习时项目的结构图:  Context ->DbContext; Entity-> 对应数据库的实体;  CoreMvc-> MVC

 

几段相关的代码:

    public class DataContext : DbContext
    {
        public DataContext()
        {

        }

        public DataContext(DbContextOptions<DataContext> options) : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //base.OnModelCreating(modelBuilder);
            modelBuilder.ApplyConfiguration(new UserConfiguration());
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
        }

        #region Entity

        public DbSet<User> User { get; set; }

        #endregion
    }
    public partial class User
    {
        public string ID { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        public Nullable<DateTime> Birthday { get; set; }

        public string Remark { get; set; }
    }

    public partial class UserConfiguration : IEntityTypeConfiguration<User>
    {
        public void Configure(EntityTypeBuilder<User> builder)
        {
            builder.ToTable("S_S_User");
            builder.HasKey(x => x.ID);
            builder.Property(x => x.ID).HasColumnName("ID").IsRequired(true).HasColumnType("nvarchar(50)");
            builder.Property(x => x.Name).HasColumnName("Name").HasColumnType("nvarchar(50)");
            builder.Property(x => x.Remark).HasColumnName("Remark").HasColumnType("nvarchar(max)");
            builder.Property(x => x.Birthday).HasColumnName("Birthday").HasColumnType("datetime");

        }
    }
public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        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.AddDbContext<DataContext>(options =>
            {
                options.UseSqlServer(Configuration["ConnectionStrings:localDb"]);
            });

            services.AddDbContext<McsContext>(options =>
            {
                options.UseSqlServer(Configuration["ConnectionStrings:localMcs"]);
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "areas",
                    template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
  "ConnectionStrings": {
    "localDb": "Server=.;Database=Core;User ID=sa;Password=******;",
    "localMcs": "Server=.;Database=MCS_TEST;User ID=sa;Password=******;"
  }

初始使用的命令是 dotnet ef migrations add xxx一直无法成功.最终也没弄明白是什么原因(如果有大佬了解,还望告知),

后面各种找资料,最终参考https://github.com/dotnet-architecture/eShopOnWeb 将问题解决,

新的命令:

//定位文件夹:
cd E:\项目资源库\DotNetCore\CoreMvc

//生成迁移文件:
dotnet ef migrations add init -c DataContext -p ../CoreContext/CoreContext.csproj -s
CoreMvc.csproj -o Migrations/Data

//更新数据库:
dotnet ef database update -c DataContext -p ../CoreContext/CoreContext.csproj -s CoreMvc.csproj

第一篇博客,不足的地方还请不要介意,不对的地方请吐槽

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值