code首先加入到集合?如何先用code。与库? [英] Code First adding to collections? How to use Code First with repositories?

查看:154
本文介绍了code首先加入到集合?如何先用code。与库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:这仅适用于具有资源库规模较大的项目发生。是否有使用EF4与codeFirst方法,并使用存储库人?请告诉我。

喜。即时通讯目前与EF4 codeFirst类的工作。在我的测试项目我有两个班,作者和图书(作家的书了)。我想要做的是,我有一个AddBook在我的Author类,但不会似乎工作像我不能将它添加到集合。这里是我的阶级,两条不同的异常。

 公共类图书
{
    公共虚拟INT BOOKID {搞定;组; }
    公共虚拟字符串名称{搞定;组; }
    公共虚拟作者作者{搞定;组; }
}公共类作者
{
    公共虚拟INT AUTHORID {搞定;组; }
    公共虚拟字符串名称{;组; }
    公共虚拟的ICollection<图书>图书{搞定;组; }    公共作者()
    {
        图书=新的收集和LT;图书>();
    }    公共无效AddBook(书书)
    {
        book.Author =这一点;
        Books.Add(书);
    }
}

例外:在类型'Author_4CF5D4EE9​​54712D3502C5DCDDAA549C8E5BF02A0B2133E8826A1AC5A40A15D2A属性图书不能设置,因为集合已设置为EntityCollection

我改变作者类此

 公共类作者
{
    公共虚拟INT AUTHORID {搞定;组; }
    公共虚拟字符串名称{;组; }
    公共虚拟的ICollection<图书>图书{搞定;组; }    公共无效AddBook(书书)
    {
        book.Author =这一点;
        Books.Add(书);
    }
}


  

例外:对象引用未设置为
  一个对象的一个​​实例


  
  

不能设置,因为集
  已被设置为一个EntityCollection


而这就是很自然的,我得到的异常,因为集合未设置为一个新的,但后来我拿到第一个例外。所以这是怎么用code完成第一次在EF?

也许我应该补充一点,我这可能与我的DbSet发生冲突?

 公共类的EntityContext:的DbContext,IUnitOfWork
{
    公共DbSet<作者>作者{搞定;组; }
    公共DbSet<图书>图书{搞定;组; }    保护覆盖无效OnModelCreating(模型构建器模型构建器)
    {
        modelBuilder.IncludeMetadataInDatabase = FALSE;
    }    公共无效保存()
    {
        保存更改();
    }
}


解决方案

虚拟上的所有属性在这里对我来说这个问题..不知道为什么它有这样,当我没有缩放它到我的项目,但是这就是它是一个影响..希望这可以帮助任何人说得到了同样的问题。

EDIT: This happen only on larger scale projects with repositories. Is there anybody using EF4 with CodeFirst approach and using repositories? Please advise me.

Hi. Im currently working with EF4 CodeFirst Classes. In my test project I got two classes, Author and Book (author got books). What I'm trying to do is that I have a AddBook in my Author class, but that wont seem to work like I can't Add it to the collection.. here are my classes and two different exceptions.

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

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

    public Author()
    {
        Books = new Collection<Book>();
    }

    public void AddBook(Book book)
    {
        book.Author = this;
        Books.Add(book);
    }
}

exception: The property 'Books' on type 'Author_4CF5D4EE954712D3502C5DCDDAA549C8E5BF02A0B2133E8826A1AC5A40A15D2A' cannot be set because the collection is already set to an EntityCollection.

I change the Author class to this

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

    public void AddBook(Book book)
    {
        book.Author = this;
        Books.Add(book);
    }
}

Exception: Object reference not set to an instance of an object.

cannot be set because the collection is already set to an EntityCollection.

And thats only natural that I get that exception because the Collection is not set to a new, but then I get that first exception. so how is this done with code first in EF?

maybe I should add that my It might collide with my DbSet?

public class EntityContext : DbContext, IUnitOfWork
{
    public DbSet<Author> Authors { get; set; }
    public DbSet<Book> Books { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.IncludeMetadataInDatabase = false;
    }

    public void Save()
    {
        SaveChanges();
    }
}

解决方案

virtual on all the properties was the issue here for me.. Don't get why it have a impact like that when I did scaled it up to my project but that's how it was.. Hope this helps anybody that got the same issue..

这篇关于code首先加入到集合?如何先用code。与库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆