创建自己的POCO类不会工作。指定的错误架构无效

时间:2010-10-03 19:05:40

标签: asp.net-mvc-2 entity-framework-4 poco

当我尝试创建自己的POCO类时,我收到此错误。这只是当我得到某种类型或类似的列表时,例如作者得到了书籍。但是当我使用T4时效果很好。我有点想创建自己的类,因为那时我可以添加我的AddBook()..所以我非常感谢,如果有人知道为什么..

Schema specified is not valid. Errors: 
The relationship 'EworkModel.AuthorBook' was not loaded because the type 'EworkModel.Book' is not available.
The following information may be useful in resolving the previous error:
The required property 'AuthorId' does not exist on the type 'EntityWork.Model.Book'.

我的课程看起来像这样

public class Author
{
    public virtual int AuthorId { get; set; }
    public virtual string Name { get; set; }
    public List<Book> Books { get; set; }
}

public class Book
{
    public virtual int BookId { get; set; }
    public virtual string Title { get; set; }
    public virtual Author Author { get; set; }
}

 private ObjectSet<Author> _authors;
    private ObjectSet<Book> _books;

    public EntityWorkContext()
        : base("name=EworkEntities", "EworkEntities")
    {            
        _authors = CreateObjectSet<Author>();
        _books = CreateObjectSet<Book>();

        ContextOptions.LazyLoadingEnabled = true;
    }

    public ObjectSet<Author> Authors
    {
        get
        {
            return _authors;
        }
    }

    public ObjectSet<Book> Books
    {
        get
        {
            return _books;
        }
    }

    public void Save()
    {
        SaveChanges();
    }

1 个答案:

答案 0 :(得分:0)

似乎EF正在您的Book实体中查找外键。也许你没有排除外键映射。

无论如何,如果你使用t4生成的POCO,你仍然可以通过创建一个部分类来添加自定义方法,如AddBook(),因为t4生成的类是部分的。