开发者

1 to 1 Object Relations in EF4 Code First

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 cl开发者_如何转开发ass 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);


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


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);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜