EF4 Code First 中的一对一对象关系

发布于 2024-09-16 07:35:25 字数 1289 浏览 2 评论 0原文

我有一个父对象书,该对象的属性是出版商。每次我为一本书做广告时,它都会添加一个新的出版商,即使该出版商已经存在。有人可以告诉我如何添加这本书,而不是再次添加出版商,而只需引用现有的书吗?我正在使用的代码如下...提前致谢!

public class Book
{
    public int BookID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime CreateDate { get; set; }

    public virtual Publisher Publisher { get; set; }
}

public class Publisher
{
    public int PublisherID { get; set; }
    public string Address { get; set; }
}

public class SqlCEDataStore : DbContext
{
    public DbSet<Book> Books { get; set; }
    public DbSet<Publishers> Publishers { get; set; }

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

public class TimeSinkRepository : IRepository<Book>
{
    private static SqlCEDataStore context = new SqlCEDataStore();

    public int Add(Book entity)
    {
        context.Books.Add(entity);
        return context.SaveChanges();
    }
}

var book = new Book()
{
      Title = "New Title",
      Description = "New Description",
      CreateDate = DateTime.Now,
      Publisher = new Publisher() { PublisherID = 1 }
};

var repository = new BookRepository();
var result = repository.Add(book);

I have a parent object book, and a property of that object is publisher. Everytime I ad a book, it is adding a new publisher, even if the publisher already exists. Can someone tell me how to add the book and instead of adding the publisher again, just reference an existing one? The code i am using is below... Thanks in advance!

public class Book
{
    public int BookID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime CreateDate { get; set; }

    public virtual Publisher Publisher { get; set; }
}

public class Publisher
{
    public int PublisherID { get; set; }
    public string Address { get; set; }
}

public class SqlCEDataStore : DbContext
{
    public DbSet<Book> Books { get; set; }
    public DbSet<Publishers> Publishers { get; set; }

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

public class TimeSinkRepository : IRepository<Book>
{
    private static SqlCEDataStore context = new SqlCEDataStore();

    public int Add(Book entity)
    {
        context.Books.Add(entity);
        return context.SaveChanges();
    }
}

var book = new Book()
{
      Title = "New Title",
      Description = "New Description",
      CreateDate = DateTime.Now,
      Publisher = new Publisher() { PublisherID = 1 }
};

var repository = new BookRepository();
var result = repository.Add(book);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

停顿的约定 2024-09-23 07:35:25

问题在于:

Publisher = new Publisher() { PublisherID = 1 }

对象上下文不知道这是现有的发布者。它是新创建的实体,因此对象上下文将执行插入操作。您必须说对象上下文表明发布者对象不是新创建的。一种方法是修改您的 Add 方法:

public int Add(Book entity)
{
  context.Books.Add(entity);

  // 0 means new one, other values mean existing one
  if (entity.Publisher.PublisherID > 0)
  {
    context.ObjectStateManager.ChangeObjectState(entity.Publisher, EntityState.Unchanged);
  }

  context.SaveChanges();
}

The problem is in the line:

Publisher = new Publisher() { PublisherID = 1 }

Object context doesn't know that this is existing publisher. It is newly created entity so Object context will perform insert operation. You have to say object context that the publisher object is not newly created. One way to do that is modification of your Add method:

public int Add(Book entity)
{
  context.Books.Add(entity);

  // 0 means new one, other values mean existing one
  if (entity.Publisher.PublisherID > 0)
  {
    context.ObjectStateManager.ChangeObjectState(entity.Publisher, EntityState.Unchanged);
  }

  context.SaveChanges();
}
枫林﹌晚霞¤ 2024-09-23 07:35:25

您可以通过在添加 Book 实体之前确保将 Publisher 附加到 Publishers 上下文来解决此问题(这样它就知道它是 dbcontext 中的 Publisher,而不是需要(再次)添加的新出版商)

context.Publishers.Attach(book.Publisher); // This is only possible if the Publisher is not new
context.Books.Add(book);

It you can solve this by making sure the Publisher is attached to Publishers context before adding the Book entity (this way it knows it's a Publisher from the dbcontext and not a new one that it needs to add (again))

context.Publishers.Attach(book.Publisher); // This is only possible if the Publisher is not new
context.Books.Add(book);
木緿 2024-09-23 07:35:25

问题出在这一行
Publisher = new Publisher() { PublisherID = 1 }

您应该执行 fetch 方法,如下所示
- 从上下文中获取您想要的发布者(例如,其中 id = 1)
- 将返回的对象设置为新图书对象的出版商
- 上下文应该为你解决剩下的问题。当你保存这本书时。 (不需要搞乱对象状态管理器)

祝你好运,如果你不能让它工作,请提供一些代码,我会帮助你完成它。

the problem is in this line
Publisher = new Publisher() { PublisherID = 1 }

You should do a fetch method so something like this
- Get the Publisher you want from the context (eg where id = 1)
- Set the returned object as the publisher for your new book object
- The context should sort the rest out for you. when you save the book. (no need to mess with the object state manager)

Good luck, if you cant get this working put up some code of it and i will help you though it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文