Code First EF4与N-Tier ..我真的坚持这个问题 [英] Code First EF4 with N-Tier.. I'm really stuck on this one issue

查看:51
本文介绍了Code First EF4与N-Tier ..我真的坚持这个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我最后几天一直在学习EF4。我决定使用POCO T4,直到我发现Code First CTP4,这就像是YES。所以我开始在一个简单的asp.net mvc 2项目上尝试它,它工作得很好但后来我实现了我的3层
模型与服务,存储库和DI,以找出它不会工作。我对此很陌生,我不知道我错过了什么或从哪里开始寻找。我会尝试尽可能地解释它。 

Hi. I been learning EF4 the last days. I set my mind on using the POCO T4 until I found out about Code First CTP4 and that was like YES. So I started trying it on a simple asp.net mvc 2 project and it worked like great but then I implemented this to my 3-tier model with services, repositories And DI to find out that It wont work. I'm so new to this that I have no clue what I'm missing or where to start looking. I'll try to explain it as good as I can. 

首先,这是我得到的例外。 

First of all this is the exception I'm getting. 

 

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

 

项目得到的Author和Book类看起来像这样。它们在我的Project.Model类库中

Project got Author and Book classes looking like this. They are in my Project.Model class library

 


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 List<Book>();
  }  
 }

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

推荐答案

Djean,我不是代码专家,但我注意到你应该对你的模型进行一些更改。

Djean, I am not an expert in code first, but I did notice that you shoul make some changes to your model.

删除实际位于sql server表格中的字段上的虚拟内容。 因此AuthorID将是一个实际字段,因此它不需要是虚拟的。 名称也是如此。  Books集合是虚拟的。  BookID和Title不应该是
是虚拟的。

Remove virtual on the fields that will actually be in the table in sql server.  So AuthorID will be an actual field, so it does not need to be virtual.  Same goes for Name.  The Books collection is virtual.  BookID and Title should not be virtual.

Author构造函数不需要构建Books集合,框架将为您完成。

There is no need for the Author constructor to construct a Books collection, the framework will do it for you.

public class 作者

{

  public   int AuthorId {
get < /跨度>; set ; }¥b $ b  public   string 名称{
get < /跨度>; set ; }

public class Author
{
  public int AuthorId { get; set; }
  public string Name { get; set; }

  public virtual ICollection< Book>书籍{
获取; set ; }


}



public < span style ="color:blue"> class 预订

{

  public   int BookId {
get < /跨度>; set ; }¥b $ b  public   s tring 标题{
get ; set ; }¥b $ b  public virtual 作者作者{
get ; set ; } b $ b}

 public virtual ICollection<Book> Books { get; set; }

}

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

我认为你的dbcontext类中唯一需要的就是这个。

I think the only thing you need in your dbcontext class is this.

  public class EntityContext:DbContext,IUnitOfWork

{

&NBSP; public DbSet< Author>作者{
get
; set ; }¥b $ b  public DbSet< Book>书籍{
get
; set ; }&NBSP;

}

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

现在你可以这样做:

EntityContext.Authors.Add(new {name = "Djean"});

EntityContext.Authors.Add(new {name = "Billy"});

EntityContext.Books.Add(new {Title = "My History", Author = EntityContext.Authors.Find(1) });

EntityContext.Books.Add(new {Title = "The Future", Author = EntityContext.Authors.Find(1) });

EntityContext.Books.Add(new {Title = "Great Moments", Author = EntityContext.Authors.Find(2) });

EntityContext.Books.Add(new {Title = "Markets", Author = EntityContext.Authors.Find(2) });

EntityContext.SaveChanges();

我确信Rowan会在这里发言,因为他是当地的专家。

I am sure Rowan will chime in here, as he is the local Expert.

我们在这个早期测试阶段,所有人都在迷雾中徘徊,所以坚持下去。 我喜欢它。

We are all stuggling in the fog here in this early beta, so stick with it.  I love it.

HTH,

Terrence


这篇关于Code First EF4与N-Tier ..我真的坚持这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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