EF6包括不起作用

时间:2014-10-20 20:49:28

标签: .net entity-framework

我有一个Book类,它在子表中有一个Book Versions。当我正在编写书籍时,BookVersions没有被提取,但是其他相关的表格被提取。我知道你会看到源代码后会说:“默认构造函数在哪里?”但我试图添加它,但它也没有用。

当我尝试手动执行查询时,按预期返回BookVersion。

其他信息:  我使用的是MySql.Data.Entity 6.9.3版本,EF6

Sample run

查询以检索 数据

选择 Project1IdProject1NameProject1AuthorNameProject1AddedDateProject1ActiveProject1C1Project1Id1Project1VersionNameProject1BookDataProject1BookCoverProject1Active1Project1BookId FROM(选择 Limit1IdLimit1NameLimit1AuthorNameLimit1AddedDateLimit1ActiveExtent2Id AS Id1Extent2VersionNameExtent2BookDataExtent2BookCoverExtent2Active AS Active1Extent2BookId, CASE WHEN(Extent2Id IS NULL)THEN(NULL)ELSE(1)END AS C1 FROM(选择 Extent1IdExtent1NameExtent1AuthorNameExtent1AddedDateExtent1Active 来自Books AS Extent1  WHERE 3 = Extent1Id LIMIT 1)AS Limit1 LEFT OUTER JOIN BookVersions AS Extent2 ON Limit1Id = Extent2BookId)AS Project1  订购 Project1Id ASC, Project1C1 ASC

- 执行于2014年10月21日09:22:41 +03:00

- 在14毫秒内完成,结果为:EFMySqlDataReader

选择 Extent1IdExtent1VersionNameExtent1BookDataExtent1BookCoverExtent1ActiveExtent1BookId 来自BookVersions AS Extent1  WHERE 3 = Extent1BookId LIMIT 1

- 执行于2014年10月21日09:22:41 +03:00

- 在7毫秒内完成,结果为:EFMySqlDataReader

选择 Extent1IdExtent1NoteExtent1StartDateExtent1ExpirationDateExtent1OwnUserIdExtent1WhoGaveIdExtent1BookId 来自UserBooks AS Extent1  WHERE Extent1BookId = @ 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);

    }

}

0 个答案:

没有答案