具有关系ID的EFCore保存实体

时间:2019-06-08 08:48:40

标签: c# entity-framework asp.net-core entity-framework-core

我正在尝试保存一个实体,但是该实体与另一个实体有关,在这个实体中我只有ID。例如,给出以下结构:

<div class="chart-gauge"></div>

我从客户那里收到了一本新书,要保存到数据库。本书仅包含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; } } TitleLibraryId(这是DTO)。 现在,我想保存这本书,与图书馆和书架建立关系,而无需检索完整的对象BookshelfIdLibrary,该如何轻松实现呢?

例如,如果我想创建一个新的图书馆并将其与一些已经存在的书联系起来怎么办?我应该怎么做才能实现它?

3 个答案:

答案 0 :(得分:1)

我想保存这本书,并与Library and Bookshelf建立关系,而不检索整个对象Library and Bookshelf

只需在书本类中添加一个属性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更新书上的图书馆属性

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

答案 1 :(得分: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
}

我认为您编写模型的方式意味着:没有Book不能创建Library,但是没有Book可以创建库。

答案 2 :(得分: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; }
}

使用新图书馆和新书架保存新书。

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

预期结果

enter image description here