ABC getting started series (3) -- create entity on the domain layer-C #. Net Tutorial

Source: Internet
Author: User
This section deals with the domain layer. First, we need to have a one-to-one correspondence between the architecture of the ABP and the solution created from the template. There is a code generator on the Internet to simplify our task, but it is not recommended for beginners. This section deals with the domain layer. First, we need to have a one-to-one correspondence between the architecture of the ABP and the solution created from the template. There is a code generator on the Internet to simplify our task, but it is not recommended for beginners.

I. First, let's take a look at the structure of the ABP system.

To create a Task, you must save the creation time. you can implement this general function by implementing IHasCreationTime in the audit module. The code is as follows:

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      }}

TaskState state enumeration is defined. Added the AssignedPerson navigation attribute to save the assignment task to a user. The [Required] and [MaxLength] features are used for input validation.

3. After defining the object, we need to define the DbSet corresponding to the object in DbContext to migrate the application Code First Data. Find our basic service layer, that is, in the project ending with EntityFramework, find the DbContext class, and add the following code

//TODO: Define an IDbSet for your Entities... public IDbSet
 
   Tasks { get; set; }
 

4. perform Code First data migration.

Open the package manager console and select the project corresponding to Entityframework for the default project. Execute Add-Migration Add_Task_Entity to create the Migration.

After the creation is successful, a class file in the _ Add_Task_Entity format will be created in the Migrations folder. If you observe, we will find that there is a SeedData folder under the Migrations folder. as the name suggests, the classes in this folder are mainly used to pre-set the SeedData. We can refer to the existing class writing method to preset two tasks. Create the DefaultTestDataForTask class. the code is as follows:

namespace LearningMpaAbp.Migrations.SeedData{public class DefaultTestDataForTask{    private readonly LearningMpaAbpDbContext _context;    private static readonly List
 
   _tasks;    public DefaultTestDataForTask(LearningMpaAbpDbContext context)    {        _context = context;    }    static DefaultTestDataForTask()    {        _tasks = new List
  
   ()        {            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)          {              _context.Tasks.Add(task);                _context.SaveChanges();               }             }         }         }
  
 

Then add the following code to the Seed method in the Configuration class.

new DefaultTestDataForTask(context).Create();

In the package manager console, enter Update-Database and press enter to perform migration. After the execution is successful, check the database. the Tasks table is created successfully and two test data items exist in the table.

Now, the Task object class is successfully created.
The source code has been uploaded to Github-LearningMpaAbp for your reference.
A series of open-source directories-practical drills for learning the ABP framework

The above is the content for creating entities at the domain layer in the ABC getting started series (3). For more information, see The PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.