【EF Code First】 一对一、一对多的多重关系配置

这里使用相册Album和图片Picture的关系做示例

1,Album与Picture最基本的关系是1-n(一个相册可以有多张图片)

这时Album、Picture实体类可以这么定义

   /// <summary>
/// 相册
/// </summary>
public class Album
{
public int ID { get; set; }
/// <summary>
/// 标题
/// </summary>
public string Title { get; set; }
public DateTime CreateTime { get; set; }
/// <summary>
/// 拥有者
/// </summary>
public virtual User Owner { get; set; }
}

  

/// <summary>
/// 图片
/// </summary>
public class Picture
{
public long ID { get; set; } public string Title { get; set; } public string Uri { get; set; } public DateTime CreateTime { get; set; } /// <summary>
/// 所属相册
/// </summary>
public virtual Album Album { get; set; }
}

生成的表结构

【EF Code First】 一对一、一对多的多重关系配置

2,后来就要改需求了,相册要加一个封面

于是Album与Picture的关系就加了一个1-1(一个相册只有一个封面)

这样就需要配置一下实体关系来区分属性之间的关系

Album和Picture类做一些修改

/// <summary>
/// 相册
/// </summary>
public class Album
{
public int ID { get; set; }
/// <summary>
/// 标题
/// </summary>
public string Title { get; set; }
public DateTime CreateTime { get; set; }
/// <summary>
/// 拥有者
/// </summary>
public virtual User Owner { get; set; }
/// <summary>
/// 封面
/// </summary>
public virtual Picture Cover { get; set; }
/// <summary>
/// 相册下的图片列表
/// </summary>
public virtual ICollection<Picture> Pictures { get; set; }
}

  

 /// <summary>
/// 图片
/// </summary>
public class Picture
{
public long ID { get; set; } public string Title { get; set; } public string Uri { get; set; } public DateTime CreateTime { get; set; } /// <summary>
/// 所属相册
/// </summary>
public virtual Album Album { get; set; }
}

然后添加一个映射类

 public class AlbumMap:EntityTypeConfiguration<Album>
{
public AlbumMap() {
this.HasMany<Picture>(a => a.Pictures).WithRequired(p=>p.Album);
}
}

EF上下文类中重写方法

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();//移除复数表名的契约
modelBuilder.Configurations.Add(new AlbumMap());
}

数据库迁移时生成的代码:

 public partial class AlbumAddCover : DbMigration
{
public override void Up()
{
DropForeignKey("dbo.Picture", "Album_ID", "dbo.Album");
DropIndex("dbo.Picture", new[] { "Album_ID" });
AddColumn("dbo.Album", "Cover_ID", c => c.Long());
AlterColumn("dbo.Picture", "Album_ID", c => c.Int(nullable: false));
CreateIndex("dbo.Album", "Cover_ID");
CreateIndex("dbo.Picture", "Album_ID");
AddForeignKey("dbo.Album", "Cover_ID", "dbo.Picture", "ID");
AddForeignKey("dbo.Picture", "Album_ID", "dbo.Album", "ID", cascadeDelete: true);
} public override void Down()
{
DropForeignKey("dbo.Picture", "Album_ID", "dbo.Album");
DropForeignKey("dbo.Album", "Cover_ID", "dbo.Picture");
DropIndex("dbo.Picture", new[] { "Album_ID" });
DropIndex("dbo.Album", new[] { "Cover_ID" });
AlterColumn("dbo.Picture", "Album_ID", c => c.Int());
DropColumn("dbo.Album", "Cover_ID");
CreateIndex("dbo.Picture", "Album_ID");
AddForeignKey("dbo.Picture", "Album_ID", "dbo.Album", "ID");
}
}

最终表结构

【EF Code First】 一对一、一对多的多重关系配置

上一节:【EF Code First】 一对多、多对多的多重关系配置

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

上一篇:np2016课程总结


下一篇:【转发】SSH无密码登录的配置