我有一个Book类,它在子表中有一个Book Versions。当我正在编写书籍时,BookVersions没有被提取,但是其他相关的表格被提取。我知道你会看到源代码后会说:“默认构造函数在哪里?”但我试图添加它,但它也没有用。
当我尝试手动执行查询时,按预期返回BookVersion。
其他信息: 我使用的是MySql.Data.Entity 6.9.3版本,EF6
查询以检索 数据
选择
Project1
。Id
,
Project1
。Name
,
Project1
。AuthorName
,
Project1
。AddedDate
,
Project1
。Active
,
Project1
。C1
,
Project1
。Id1
,
Project1
。VersionName
,
Project1
。BookData
,
Project1
。BookCover
,
Project1
。Active1
,
Project1
。BookId
FROM(选择
Limit1
。Id
,
Limit1
。Name
,
Limit1
。AuthorName
,
Limit1
。AddedDate
,
Limit1
。Active
,
Extent2
。Id
AS Id1
,
Extent2
。VersionName
,
Extent2
。BookData
,
Extent2
。BookCover
,
Extent2
。Active
AS Active1
,
Extent2
。BookId
,
CASE WHEN(Extent2
。Id
IS NULL)THEN(NULL)ELSE(1)END AS C1
FROM(选择
Extent1
。Id
,
Extent1
。Name
,
Extent1
。AuthorName
,
Extent1
。AddedDate
,
Extent1
。Active
来自Books
AS Extent1
WHERE 3 = Extent1
。Id
LIMIT 1)AS Limit1
LEFT OUTER JOIN BookVersions
AS Extent2
ON Limit1
。Id
= Extent2
。BookId
)AS Project1
订购
Project1
。Id
ASC,
Project1
。C1
ASC
- 执行于2014年10月21日09:22:41 +03:00
- 在14毫秒内完成,结果为:EFMySqlDataReader
选择
Extent1
。Id
,
Extent1
。VersionName
,
Extent1
。BookData
,
Extent1
。BookCover
,
Extent1
。Active
,
Extent1
。BookId
来自BookVersions
AS Extent1
WHERE 3 = Extent1
。BookId
LIMIT 1
- 执行于2014年10月21日09:22:41 +03:00
- 在7毫秒内完成,结果为:EFMySqlDataReader
选择
Extent1
。Id
,
Extent1
。Note
,
Extent1
。StartDate
,
Extent1
。ExpirationDate
,
Extent1
。OwnUserId
,
Extent1
。WhoGaveId
,
Extent1
。BookId
来自UserBooks
AS Extent1
WHERE Extent1
。BookId
= @ EntityKeyValue1
- EntityKeyValue1:' 3' (Type = Int32)
- 执行于2014年10月21日09:22:50 +03:00
- 在1 ms内完成,结果为:EFMySqlDataReader
源代码如下:
public class Book : BaseEntity
{
private ICollection<UserBook> _userBook;
private ICollection<BookVersion> _bookVersions;
/// <summary>
/// Gets or sets a value Book name
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets a value indicating Book's author name
/// </summary>
public string AuthorName { get; set; }
/// <summary>
/// Gets or sets a value indicating Book's added date
/// </summary>
public DateTime AddedDate { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the Book is active
/// </summary>
public bool Active { get; set; }
/// <summary>
/// Gets or sets who can see the Book
/// </summary>
public virtual ICollection<UserBook> UserBooks
{
get { return _userBook ?? (_userBook = new List<UserBook>()); }
set { _userBook = value; }
}
/// <summary>
/// Gets or sets the versions of the Book
/// </summary>
public virtual ICollection<BookVersion> BookVersions
{
get { return _bookVersions ?? (_bookVersions = new List<BookVersion>()); }
set { _bookVersions = value; }
}
}
public class BookVersion : BaseEntity
{
private Book _book;
public BookVersion() { }
/// <summary>
/// Gets or sets a value indicating VersionName
/// </summary>
public string VersionName { get; set; }
/// <summary>
/// Gets or sets Book Data.
/// <remarks>It stores the File as Byte. It is a discrete form of the Book</remarks>
/// </summary>
public byte[] BookData { get; set; }
/// <summary>
/// Gets or sets a value indicating Book cover
/// </summary>
public byte[] BookCover { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the version is active
/// </summary>
public bool Active { get; set; }
/// <summary>
/// Gets or sets a value indicating book identifier
/// </summary>
public int BookId { get; set; }
/// <summary>
/// Gets or sets the version belongs to whom
/// </summary>
public virtual Book OfBook
{
get { return _book ?? (_book = new Book()); }
set { _book = value; }
}
}
映射是:
public class BookMapping : EntityTypeConfiguration<Book>
{
public BookMapping()
{
this.HasKey(b => b.Id);
this.Property(b => b.Name).HasMaxLength(255);
this.Property(b => b.AuthorName).IsOptional();
this.Property(b => b.AuthorName).HasMaxLength(255);
this.Property(b => b.Name).IsRequired();
this.Property(b => b.Active).IsRequired();
}
}
public class BookVersionMapping : EntityTypeConfiguration<BookVersion>
{
public BookVersionMapping()
{
this.HasKey(n => n.Id);
this.Property(n => n.BookData).IsRequired();
this.Property(n => n.VersionName).IsRequired();
this.Property(n => n.VersionName).HasMaxLength(255);
this.Property(n => n.BookCover).IsOptional();
this.HasRequired(bv => bv.OfBook).WithMany(b => b.BookVersions).HasForeignKey(bv => bv.BookId).WillCascadeOnDelete(true);
}
}