Analysis on the data migration and data migration of the master node.
This article describes how to migrate an object class to a database table in the ABP framework.
1. It is the architecture of abp. To create an object class and migrate data to a database, perform operations in the domain layer.
2. Check the solution, for example. Core is the application (application service layer), core (domain layer), EntityFramework (infrastructure layer), web and webapi (Web and presentation) that need to be modified from top to bottom)
3. Create a Tasks folder under core and a Task class under the folder.
using System;using System.ComponentModel.DataAnnotations;using System.ComponentModel.DataAnnotations.Schema;using Abp.Domain.Entities;using Abp.Domain.Entities.Auditing;using Abp.Timing;using LearningMpaAbp.Authorization.Users;namespace LearningMpaAbp.Tasks{ public class Task : Entity, IHasCreationTime { public const int MaxTitleLength = 256; public const int MaxDescriptionLength = 64 * 1024;//64kb public long? AssignedPersonId { get; set; } [ForeignKey("AssignedPersonId")] public User AssignedPerson { get; set; } [Required] [MaxLength(MaxTitleLength)] public string Title { get; set; } [Required] [MaxLength(MaxDescriptionLength)] public string Description { get; set; } public TaskState State { get; set; } public DateTime CreationTime { get; set; } public Task() { CreationTime = Clock.Now; State = TaskState.Open; ; } public Task(string title, string description = null) : this() { Title = title; Description = description; } } public enum TaskState : byte { Open = 0, Completed = 1 }}
4. Next, add it to XXXDbContext in EntityFramework.
Note: Add reference
Using LearningMpaAbp. Tasks;
//TODO: Define an IDbSet for your Entities... public IDbSet<Task> Tasks { get; set; }
5. Finally, perform data migration.
Open the Package Manager Console and select the project corresponding to Entityframework for the default project. RunAdd-Migration Add_Task_Entity, Create a migration
After the creation is successful, a new time + Add_Task_Entity class will be generated under Migrations
In the Package Manager Console, enterUpdate-Database, Press enter to execute migration. After the execution is successful, check the database. The Tasks table is created successfully.
If you want to inject default data
Create the DefaultTestDataForTask class of the preset data in the SeedData folder of the Migrations folder.
using System.Collections.Generic;using System.Linq;using LearningMpaAbp.EntityFramework;using LearningMpaAbp.Tasks;namespace LearningMpaAbp.Migrations.SeedData{ public class DefaultTestDataForTask { private readonly LearningMpaAbpDbContext _context; private static readonly List<Task> _tasks; public DefaultTestDataForTask(LearningMpaAbpDbContext context) { _context = context; } static DefaultTestDataForTask() { _tasks = new List<Task>() { new Task("Learning ABP deom", "Learning how to use abp framework to build a MPA application."), new Task("Make Lunch", "Cook 2 dishs") }; } public void Create() { foreach (var task in _tasks) { if (_context.Tasks.FirstOrDefault(t => t.Title == task.Title) == null) { _context.Tasks.Add(task); } _context.SaveChanges(); } } }}
Then add
New DefaultTestDataForTask (context). Create ();
Package Manager Console, enterUpdate-Database, Press enter to execute migration. After the execution is successful, check the database. The Tasks table is created successfully and two test data items exist in the table.
Above...
Reference
Http://www.jianshu.com/p/fde465ae599d