添加實體
在類庫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實體中。
添加數據庫遷移
我們的模型實體已經發生變更,所以我們需要添加一個新的遷移類:
然后是遷移生成的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視圖中,然后運行項目
到目前為止就是ABP最基本的用法了。
有什么不清楚的話,可以加群討論
-返回目錄- ABP打造一個《電話簿項目》
交流QQ群:104390185


