在ABP中創建Person實體類


經過之前的准備目前我們的項目,終於可以搞正式的開發工作了。

創建實體Person

在Core類庫中添加Person類

image

/// <summary>
    /// 聯系人
    /// </summary>
    public class Person : FullAuditedEntity
    {
        /// <summary>
        /// 姓名
        /// </summary>
        [Required]
        [MaxLength(PhoneBookConsts.MaxNameLength)]
        public virtual string Name { get; set; }


        /// <summary>
        /// 郵箱地址
        /// </summary>
        [MaxLength(PhoneBookConsts.MaxEmailAddressLength)]
        public virtual string EmailAddress { get; set; }
    }

然后在PhoneBookConsts類中添加幾個常量:

/// <summary>
    /// 電話薄項目的使用常量
    /// </summary>
    public static class PhoneBookConsts
    {
        public const string LocalizationSourceName = "PhoneBook";

        /// <summary>
        /// 名字最大長度
        /// </summary>
        public const int MaxNameLength = 32;

        /// <summary>
        /// 郵件地址最大長度
        /// </summary>
        public const int MaxEmailAddressLength = 255;
    }

提示:

person的主鍵為INT類型。

他集成了FullAuditedEntity,它包含了創建、修改、刪除、審核屬性。這里的刪除是指“軟刪除”。

當我們刪除一個人的時候,他不是從數據庫中刪除,而是被標記為刪除狀態。我們創建了一個常量為MaxNameLength的屬性,這么做有一個好處,因為稍后我們可以將相同的值用不到不同的實體屬性上。

 

然后這個時候就可以進行數據庫遷移了會在數據庫生成Person這個表。

P3M21L()G{V)3G9GUW5K~BC

但是我偏不。為嘛呢。我可是一個有代碼生成器的男人*(實力硬廣)

使用代碼生成器

下載地址:https://visualstudiogallery.msdn.microsoft.com/b1364d50-a445-433f-971c-373b402b6694

這個的安裝過程就不說 了。要是你不會。。加群吧,我教你{}B0Q48VCYTX@{UBEZ@DQQ8

使用之前的准備工作

ABP的代碼生成器是為了讓大家更好的工作,所以在有些地方是做了處理的。我們就先來准備點東西吧

首先准備個常量類,放到Core項目中的根目錄

/// <summary>
    /// 一些程序中會使用到的通用常量
    /// </summary>
    public static class YoYoCMSConsts
    {

        
     /// <summary>
     /// 顯示名稱的最大長度
     /// </summary>
        public const int MaxDisplayNameLength = 64;
       
    /// <summary>
    /// 用戶名的最大長度
    /// </summary>
        public const int MaxUserNameLength = 32;

        /// <summary>
        /// 數據庫架構名
        /// </summary>
        public static class SchemaName
        {
            /// <summary>
            /// 基礎設置
            /// </summary>
            public const string Basic = "Basic";

            /// <summary>
            /// 模塊管理
            /// </summary>
            public const string Moudle = "Moudle";

            /// <summary>
            /// 網站設置
            /// </summary>
            public const string WebSetting = "WebSetting";
            /// <summary>
            /// 用於對多對表關系的結構
            /// </summary>
            public const string HasMany = "HasMany";

            /// <summary>
            /// 業務
            /// </summary>
            public const string Business = "Business";

        }

    }

創建一個PagedAndSortedInputDto.cs類

public class PagedAndSortedInputDto : IPagedResultRequest, ISortedResultRequest
    {
        public string Sorting { get; set; }

        public PagedAndSortedInputDto()
        {
            MaxResultCount = PhoneBookConsts.DefaultPageSize;
        }

        [Range(1, PhoneBookConsts.MaxPageSize)]
        public int MaxResultCount { get; set; }

        [Range(0, int.MaxValue)]
        public int SkipCount { get; set; }

      
    }

 

打開nuget管理器

image

將ef、System.Linq.Dynamic安裝到application中

然后生成代碼

怎么使用呢。找到我們的web項目。

然后如下圖所示:

右鍵web項目,添加、新建基建項目

image

選擇”添加模塊功能“

image

然后選擇Person實體

image

然后可以看到生成出來的代碼:

image

首先將EntityFramework中的EntityMapper剪切到YoYoCMS.PhoneBook.EntityFramework中。

然后根據PersonCfg.cs中的todo:提示

 

public class PersonCfg : EntityTypeConfiguration<Person>
    {
        public PersonCfg ()
        {
                    ToTable("Person", YoYoCMSConsts.SchemaName.Basic);
 
      //todo: 需要將以下文件注入到YoYoCMS.PhoneBook.PersonsDbContext中

    //  public IDbSet<Person> Persons { get; set; }
 //   modelBuilder.Configurations.Add(new PersonCfg());




            // 姓名
            Property(a => a.Name).HasMaxLength(32);
            // 郵箱地址
            Property(a => a.EmailAddress).HasMaxLength(255);
        }
    }

將public IDbSet<> Person> Persons { get; set; }和modelBuilder.Configurations.Add(new PersonCfg());

打開”PhoneBookDbContext.cs”文件在//TODO: Define an IDbSet for your Entities...下面粘貼上

public IDbSet<> Person> Persons { get; set; }

手動創建方法,

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
         


modelBuilder.Configurations.Add(new PersonCfg());
base.OnModelCreating(modelBuilder);


         }

最后的效果:

public class PhoneBookDbContext : AbpZeroDbContext<Tenant, Role, User>
    {
        //TODO: Define an IDbSet for your Entities...

        public IDbSet<Person> Persons { get; set; }

        /* NOTE: 
         *   Setting "Default" to base class helps us when working migration commands on Package Manager Console.
         *   But it may cause problems when working Migrate.exe of EF. If you will apply migrations on command line, do not
         *   pass connection string name to base classes. ABP works either way.
         */
        public PhoneBookDbContext()
            : base("Default")
        {

        }

        /* NOTE:
         *   This constructor is used by ABP to pass connection string defined in PhoneBookDataModule.PreInitialize.
         *   Notice that, actually you will not directly create an instance of PhoneBookDbContext since ABP automatically handles it.
         */
        public PhoneBookDbContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {

        }

        //This constructor is used in tests
        public PhoneBookDbContext(DbConnection connection)
            : base(connection, true)
        {
 
        }

        
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new PersonCfg());

         }
    }

然后再次打開

image

將以上代碼剪切到Core類庫中。

將下圖的代碼,剪切到application類庫中

image

 

然后我們再一步步的解釋這些分別怎么用

本地化多語言

image

將PersonReadMe.cs中的文件

image

復制粘貼到“PhoneBook-zh-CN.xml”文件中.

注意這個文件中不能有重復的name,否則會報錯

其他的提示信息同理,放到對應的文件中。

//TODO:多頁面后端的導航欄目設計

    /*
    //無次級導航屬性
       var person=new MenuItemDefinition(
                PersonAppPermissions.Person,
                L("Person"),
                url:"Mpa/PersonManage",
                icon:"icon-grid",
                 requiredPermissionName: PersonAppPermissions.Person
                );

*/
                //有下級菜單
            /*

               var person=new MenuItemDefinition(
                PersonAppPermissions.Person,
                L("Person"),            
                icon:"icon-grid"                
                );

                person.AddItem(
            new MenuItemDefinition(
            PersonAppPermissions.Person,
            L("Person"),
            "icon-star",
            "person",
            requiredPermissionName: PersonAppPermissions.Person));
    

            
            */

    //配置權限模塊初始化

 
        
    
 //TODO:★需要到請將以下內容剪切到YoYoCMSApplicationModule
 //   Configuration.Authorization.Providers.Add<PersonAppAuthorizationProvider>();


 

//TODO:★請將以下內容剪切到CORE類庫的Localization/Source/zh-cn.xml
/*
    <!-- 聯系人管理 -->


        <text name="    PersonHeaderInfo" value="聯系人信息列表" />
            <text name="PersonDeleteWarningMessage" value="聯系人名稱: {0} 將被刪除,是否確定刪除它。" />

                <text name="PersonName" value="聯系人" />

<!--//用於表格展示的數據信息的標題-->
                    <text name="Name" value="姓名" />
                     <text name="EmailAddress" value="郵箱地址" />
                     <text name="LastModificationTime" value="最后編輯時間" />
                     <text name="CreationTime" value="創建時間" />
                 

    <text name="Person" value="聯系人管理" />
    <text name="CreatePerson" value="新增聯系人" />
    <text name="UpdatePerson" value="更新聯系人" />
    <text name="DeletePerson" value="刪除聯系人" />
*/

/*
    <!-- 聯系人english管理 -->
            <text name="    PersonHeaderInfo" value="聯系人List" />


    <text name="Person" value="ManagementPerson" />
    <text name="CreatePerson" value="CreatePerson" />
    <text name="UpdatePerson" value="UpdatePerson" />
    <text name="DeletePerson" value="DeletePerson" />
*/

文中涉及權限一塊的內容,因為沒有做權限頁面左右我就刪除掉了。保證他不出錯就可以了。

image

生成遷移數據庫

遷移命令:Add-Migration add_Person

image

然后進行update-database,數據庫的表應該就正常的ok了

 image

然后我們要添加菜單打開“PhoneBookNavigationProvider”

然后添加

var person = new MenuItemDefinition(
              PersonAppPermissions.Person,
              L("Person"),
              url: "PersonManage",
              icon: "fa fa-user-md"
              );
image

添加到users后面,這里的添加順序是受菜單的影響的說。

添加控制器,生成視圖文件

image

 

運行項目

image

可以看到我們的聯系人管理。這個時候呢,我們的聯系人管理缺沒有被選中,下一步就開始改造視圖的CSS效果

制作選中效果

 

回到我們的菜單導航頁面

var person = new MenuItemDefinition(
              PersonAppPermissions.Person,
              L("Person"),
              url: "PersonManage",
              icon: "fa fa-user-md"
              );

image

然后回到視圖頁面

image做一點小小的修改。

刷新后,發現ok了。

image

 

the end

-返回目錄-  ABP打造一個《電話簿項目》


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM