2

I'm trying to save an entity, but this entity is related to another one and of this one I only have the id. For example, giving this structure:

public class Library
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Book> Books { get; set; }
}

public class Book
{
    public int Id { get; set; }
    public int LibraryId { get; set; }
    public string Title { get; set; }
    public Bookshelf Bookshelf { get; set; }
}

public class Bookshelf
{
    public int Id { get; set; }
    public string Color { get; set; }
}

I receive from the client a new book to save to the db. This book only contains the Title, the LibraryId and the BookshelfId (it's a DTO). Now I want to save this book, creating the relationship with Library and Bookshelf, without retrieving the full object Library and Bookshelf, how can I do it easily?

What if, for example, I want to create a new library, and relate it to some already existing books? What I should do to achieve it?

4

3 回答 3

2

我想保存这本书,创建与图书馆和书架的关系,而不检索完整的对象图书馆和书架

只需在您的图书类中添加一个属性 BookshelfId。

public class Book
{
    public int Id { get; set; }
    public int LibraryId { get; set; }
    public string Title { get; set; }
    public Bookshelf Bookshelf { get; set; }
    public int BookshelfId { get; set; }
}

然后你可以这样做:

DbContext.Add(new Book { LibraryId = 1, BookshelfId = 1});
DbContext.SaveChanges();

例如,如果我想创建一个新图书馆,并将其与一些现有书籍相关联怎么办?

在书上添加图书馆导航属性

public class Book
{
    public int Id { get; set; }
    public Library Library { get; set; }
    ...
}

然后您可以仅使用它们的 id 更新书籍上的 library 属性

var library = new Library { Name = "My library" };
DbContext.Libraries.Add(library);

var bookIds = new int[] {1, 2};

foreach(var bookId in bookIds) {
    var book = new Book { Id= bookId, Library = library};
    DbContext.Attach(book);
    DbContext.Entry(book).Property(b => b.LibraryId).IsModified = true;
}

DbContext.SaveChanges();
于 2019-06-08T09:23:16.560 回答
0

这个例子我在不获取任何数据的情况下创建了一个新图书馆、新书架和新书。

我的课程:

public class Library
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Book> Books { get; set; }
}

public class Bookshelf
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }
    public string Color { get; set; }
}

public class Book
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }
    public int LibraryId { get; set; }
    public string Title { get; set; }

    public int BookshelfId { get; set; }

    [ForeignKey("BookshelfId")]
    public virtual Bookshelf Bookshelf { get; set; }

    [ForeignKey("LibraryId")]
    public virtual Library Library { get; set; }
}

用新图书馆和新 Bookshekf 保存新书。

var contextDb = new LabContextDb();

var newLibrary = new Library()
{                
   Id = 1,
   Name = "My new library",
};

newLibrary = contextDb.Libraries.Add(newLibrary).Entity;

 var newBookshelf = new Bookshelf() { Id = 2, Color = "Blue" };

 newBookshelf = contextDb.Bookshelves.Add(newBookshelf).Entity;

 var newBook = new Book()
 {
    Id = 5,
    Title = "My New Book",
    LibraryId = newLibrary.Id,
    Bookshelf = newBookshelf
  };

 contextDb.Books.Add(newBook);
 contextDb.SaveChanges();

预期结果

在此处输入图像描述

于 2019-06-08T09:56:18.040 回答
0

您的Book模型将略有变化以反映LibraryBookShelf模型的关系

public class Book
{
  public int Id { get; set; }
  public string Title { get; set; }

  public int LibraryId { get; set; }
  public Library Library {get;set;} //navigation property to Library

  public Bookshelf Bookshelf { get; set; }
  public Bookshelf Bookshelf {get;set;} //navigation property to Bookshelf
}

我看到您编写模型的方式意味着:没有 aBook就无法创建 a Library,但没有 a 就可以创建库Book

于 2019-06-08T09:36:05.357 回答