创建一个Phone实体,完成多页面的电话簿项目

添加实体

在类库CORE中添加:

[Table("PbPhones")]
public class Phone : CreationAuditedEntity<long>
{
    public const int MaxNumberLength = 16;

    [ForeignKey("PersonId")]
    public virtual Person Person { get; set; }
    public virtual int PersonId { get; set; }

    [Required]
    public virtual PhoneType Type { get; set; }

    [Required]
    [MaxLength(MaxNumberLength)]
    public virtual string Number { get; set; }
}

电话号码存在表“PbPhones”中,他的主键是long自增,然后也带审核属性的字段。和person的关系为一对多。

添加phone实体导航属性到person实体中。

[Table("PbPersons")]
public class Person : FullAuditedEntity
{
    //...other properties

    public virtual ICollection<Phone> Phones { get; set; }
}

再添加一个枚举类型

/// <summary>
/// 电话类型
/// </summary>
    public enum PhoneType : byte
    {/// <summary>
    /// 移动
    /// </summary>
        Mobile,
        /// <summary>
        /// 住宅
        /// </summary>
        Home,
        /// <summary>
        /// 商业
        /// </summary>
        Business
    }

最后,在 DbContext中我们增加了一个 DbSet 属性的Phone。

再将Phone实体,添加到Person实体中。

添加数据库迁移

我们的模型实体已经发生变更,所以我们需要添加一个新的迁移类:

image

然后是迁移生成的PbPhones表

public partial class Add_Phones : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.PbPhone",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        PersonId = c.Int(nullable: false),
                        Type = c.Byte(nullable: false),
                        Number = c.String(nullable: false, maxLength: 16),
                        CreationTime = c.DateTime(nullable: false),
                        CreatorUserId = c.Long(),
                        Phone_Id = c.Long(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("Basic.Person", t => t.PersonId, cascadeDelete: true)
                .ForeignKey("dbo.PbPhone", t => t.Phone_Id)
                .Index(t => t.PersonId)
                .Index(t => t.Phone_Id);
            
        }
        
        public override void Down()
        {
            DropForeignKey("dbo.PbPhone", "Phone_Id", "dbo.PbPhone");
            DropForeignKey("dbo.PbPhone", "PersonId", "Basic.Person");
            DropIndex("dbo.PbPhone", new[] { "Phone_Id" });
            DropIndex("dbo.PbPhone", new[] { "PersonId" });
            DropTable("dbo.PbPhone");
        }
    }

初始化默认数据

在EntityFramewok中添加初始化数据

private void CreatePhone()
        {
            var defaultPhone = _context.Persons.FirstOrDefault(p => p.EmailAddress == "admin@yoyocms.com");
            if (defaultPhone==null)
            {
                _context.Persons.Add(new Person()
                {
                    Name = "张三",
                    EmailAddress = "admin@yoyocms.com",
                    Phones = new List<Phone>()
                    {
                        new Phone() {Type = PhoneType.Business,Number = "87115555"},
                        new Phone() {Type = PhoneType.Home,Number = "010-1109"}
                    }
                });
            }
            var defaultPerson = _context.Persons.FirstOrDefault(p => p.EmailAddress == "lisi@yoyocms.com");
            if (defaultPerson==null)
            {
                _context.Persons.Add(new Person()
                {
                    Name = "李四",
                    EmailAddress = "lisi@yoyocms.com",
                    Phones = new List<Phone>()
                    {
                          new Phone() {Type = PhoneType.Business,Number = "88452675"},
                        new Phone() {Type = PhoneType.Home,Number = "010-441109"}
                    }
                });
            }
            _context.SaveChanges();

        }

 

这样的话,就是张三下面有2机号码。李四也有两个联系号码。

  修改分页查询

首先修改PersonListDto,添加PhoneListDto

[AutoMapFrom(typeof (Phone))]
    public class PhoneListDto : CreationAuditedEntity<long>
    {
        /// <summary>
        ///     电话类型
        /// </summary>
        public virtual PhoneType Type { get; set; }

        /// <summary>
        ///     联系号码
        /// </summary>
        public virtual string Number { get; set; }
    }

将此Dto添加到PersonListDto中

public class PersonListDto : EntityDto<int>
    {
      //额外的代码


        public Collection<PhoneListDto> phones { get; set; } 

    }

剩下的就是我们操作视图页面,将电话号码显示在页面上了。

这里我就去考虑页面好不好看的问题了。这个不是本次教程的目的。

<tr>
                            <th>电话类型</th>
                            <th>电话号码</th>
                        </tr>

                        @foreach (var phone in person.Phones)
                        {
                            <tr>
                                <td>@phone.Type</td>
                                <td>@phone.Number</td>
                            </tr>
                        }
                    </tr>

将以上信息添加到person视图中,然后运行项目

image

到目前为止就是ABP最基本的用法了。

有什么不清楚的话,可以加群讨论

-返回目录-  ABP打造一个《电话簿项目》

交流QQ群:104390185
posted @ 2016-08-18 15:40  梁桐铭  阅读(2021)  评论(0编辑  收藏  举报