EF+Code First 数据关系隐射及迁移笔记一
1.一对一关系(one to one)
1.1DataAnnotations方式
实体:书本实体,版本信息实体,一本书只能有一个版本号,版本号在没有书出版的情况下是无意义的
public class Book
{
/// <summary>
/// Id,主键
/// </summary>
[Key]
public int BookID { get; set; }
/// <summary>
/// 书籍名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 书籍类型
/// </summary>
public string Category { get; set; }
/// <summary>
/// 出版数量
/// </summary>
public int Numberofcopies { get; set; }
/// <summary>
/// 作者Id
/// </summary>
public int AuthorID { get; set; }
/// <summary>
/// 书籍价格
/// </summary>
public decimal Price { get; set; }
/// <summary>
/// 出版日期
/// </summary>
public DateTime PublishDate { get; set; }
/// <summary>
/// 评级
/// </summary>
public string Rating { get; set; }
/// <summary>
/// 版本号Id
/// </summary>
public int VersionId { get; set; }
/// <summary>
/// 一对一版本号信息
/// </summary>
public PublishInfo publish { get; set; }
/// <summary>
/// 作者信息
/// </summary>
public Author author { get; set; }
}
public class PublishInfo
{
/// <summary>
/// 版本Id
/// </summary>
[Key]
[ForeignKey("book")] 设置此字段为Book的外键
public int VersionId { get; set; }
/// <summary>
/// 版本号
/// </summary>
public string VersionNum { get; set; }
/// <summary>
/// 出版社名称
/// </summary>
public string PressName { get; set; }
/// <summary>
/// 关联的书本信息
/// </summary>
public virtual Book book { get; set; }
}
说明:
[ForeignKey("book")] 为设置外键,设置了版本信息的主键为书本实体的外键,在使用DataAnnotations方式的时候,记得要引用“System.ComponentModel.DataAnnotations”和“System.ComponentModel.DataAnnotations.Schema”命名空间
1.2Fluent API方式
在以上实体的基础上添加两个映射类代码如下:
public PublishInfoMap()
{
this.HasKey(p => p.VersionId);
this.Property(p => p.VersionId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(p => p.VersionNum).HasColumnType("nvarchar").HasMaxLength(50);
this.Property(p => p.PressName).HasColumnType("nvarchar").HasMaxLength(100);
this.ToTable(" PublishInfo");
this.HasRequired(p => p.book).WithRequiredDependent(p => p.publish);
}
public BookMap()
{
this.HasKey(b => b.BookID).Property(b => b.BookID).HasColumnName("B_Id");
this.Property(b => b.PublishDate).HasColumnType("datetime").IsRequired();
this.Property(b => b.Price).HasColumnType("decimal").IsRequired();
this.Property(b => b.Name).HasColumnType("nvarchar").HasMaxLength(200).IsRequired();
this.Property(b => b.Rating).HasColumnType("nvarchar").HasMaxLength(5).IsRequired();
this.Property(b => b.Category).HasColumnType("nvarchar").HasMaxLength(50).IsRequired();
this.HasRequired(b => b.author).WithMany(b => b.books).HasForeignKey(b => b.AuthorID);
this.ToTable("Book");
}
说明:
this.HasRequired(p => p.book).WithRequiredDependent(p => p.publish);
此段代码设置了两张表的一对一映射关系
2.一对多关系(one to More)
实体:一本书只能有一个作者,一个作者可以有多本数
public class Book
{
/// <summary>
/// Id,主键
/// </summary>
public int BookID { get; set; }
/// <summary>
/// 书籍名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 书籍类型
/// </summary>
public string Category { get; set; }
/// <summary>
/// 出版数量
/// </summary>
public int Numberofcopies { get; set; }
/// <summary>
/// 作者Id
/// </summary>
public int AuthorID { get; set; }
/// <summary>
/// 书籍价格
/// </summary>
public decimal Price { get; set; }
/// <summary>
/// 出版日期
/// </summary>
public DateTime PublishDate { get; set; }
/// <summary>
/// 评级
/// </summary>
public string Rating { get; set; }
/// <summary>
/// 版本号Id
/// </summary>
public int VersionId { get; set; }
/// <summary>
/// 一对一版本号信息
/// </summary>
public PublishInfo publish { get; set; }
/// <summary>
/// 作者信息
/// </summary>
public Author author { get; set; }
}
public class Author
{
/// <summary>
/// 主键Id
/// </summary>
public int AuthorID { get; set; }
/// <summary>
/// 姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 性别
/// </summary>
public int Sex { get; set; }
/// <summary>
/// 国籍
/// </summary>
public string Country { get; set; }
public virtual ICollection<Book> books { get; set; }
}
2.1DataAnnotations方式
[ForeignKey("AuthorID")]
public Author author { get; set; }
在book实体上设置以上代码
2.2Fluent Api方式
public BookMap()
{
this.HasKey(b => b.BookID).Property(b => b.BookID).HasColumnName("B_Id");
this.Property(b => b.PublishDate).HasColumnType("datetime").IsRequired();
this.Property(b => b.Price).HasColumnType("decimal").IsRequired();
this.Property(b => b.Name).HasColumnType("nvarchar").HasMaxLength(200).IsRequired();
this.Property(b => b.Rating).HasColumnType("nvarchar").HasMaxLength(5).IsRequired();
this.Property(b => b.Category).HasColumnType("nvarchar").HasMaxLength(50).IsRequired();
this.HasRequired(b => b.author).WithMany(b => b.books).HasForeignKey(b => b.AuthorID);
this.ToTable("Book");
}
public AuthorMap()
{
this.ToTable("Author");
this.HasKey(a => a.AuthorID);
this.Property(a => a.Name).HasColumnType("nvarchar").HasMaxLength(50);
this.Property(a => a.Country).HasColumnType("nvarchar").HasMaxLength(30);
this.HasMany(a => a.books).WithRequired(a => a.author).HasForeignKey(a => a.AuthorID);
}
设置:
BookMap类和
AuthorMap类
this.HasRequired(b => b.author).WithMany(b => b.books).HasForeignKey(b => b.AuthorID);
this.HasMany(a => a.books).WithRequired(a => a.author).HasForeignKey(a => a.AuthorID);
数据迁移
启动迁移:Enable-Migrations;
添加迁移文件:Add-Migration Info(文件的名称)
迁移到数据库:Update-Database
说明:“在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。
请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - 定位指定的服务器/实例时出错)”
若出现这个错误,需要指定要迁移的项目名称:Update-Database -StartUpProjectName "ContextConfig"(你要迁移的项目名称)

浙公网安备 33010602011771号