且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

INSERT语句与Entity Framework Core中的FOREIGN KEY约束冲突

更新时间:2023-02-25 21:59:00

在您的 ApplicationDbContext中类,您必须为 Author 类和 DbSet $ c> BookPage 类:

In your ApplicationDbContext class you have to add a DbSet for the Author class and for the BookPage class :

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

    public DbSet<BookPage> BookPages { get; set; }

    public DbSet<Author> Authors { get; set; }

    ...
}

这样您就可以像这样重写代码:

So that you can rewrite your code like that :

var author = dbContext.Authors.Add(new Author()
{
    AuthorId = Guid.NewGuid().ToString(),
    FullName = "Forexample"
}).Entity;

var page = dbContext.BookPages.Add(new BookPage()
{
    BookPageId = Guid.NewGuid().ToString(),
    Number = 1
}).Entity;


var book = new Book.Book()
{
    BookId = Guid.NewGuid().ToString(),
    Pages = new List<BookPage>(),
    Text = "new book"
};
book.Pages.Add(page);
book.Author = author ;
dbContext.Books.Add(book);
dbContext.SaveChanges();

无论如何您不应该使用 Guid 作为主键(实际上是聚集索引),这在使用SQL Server时是一种不好的做法。

Anyway you shouldn't use Guid as a primary key (in fact as a clustered index), this is a bad practice using SQL Server.

您可以查看一下发布更多信息:

You can have a look at this post for more information :

  • What are the best practices for using a GUID as a primary key, specifically regarding performance?

另一种方法是使用一个身份列作为主键,并添加另一个具有唯一约束的列(它将包含唯一ID),因此您的模型看起来像这样:

An alternative is to use an identity column as the primary key and add a other column (which will contain the unique id) with a unique constraint so that your model could look like that :

public class Author
{
    public Author()
    {
        AuthorUid = Guid.NewGuid();
    }

    public int AuthorId { get; set; }
    public Guid AuthorUid { get; set; }
    public string FullName { get; set; }
}

public class Book
{
    public Book()
    {
        BookUid = Guid.NewGuid();
        Pages = new List<BookPage>();
    }

    public int BookId { get; set; }
    public Guid BookUid { get; set; }
    public List<BookPage> Pages { get; set; }
    public Author Author { get; set; }
    public string Text { get; set; }
}

public class BookPage
{
    public BookPage()
    {
        BookPageUid = Guid.NewGuid();
    }

    public int BookPageId { get; set; }
    public Guid BookPageUid { get; set; }
    public int Number { get; set; }
}

在您的 DbContext $ c >,您可以指定唯一约束:

In your DbContext, you can specify the unique constraints:

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

    public DbSet<BookPage> BookPages { get; set; }

    public DbSet<Author> Authors { get; set; }

    ...

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Author>().HasAlternateKey(a => a.AuthorUid);
        modelBuilder.Entity<Book>().HasAlternateKey(a => a.BookUid);
        modelBuilder.Entity<BookPage>().HasAlternateKey(a => a.BookPageUid);
    }
}

并添加您的新实体:

using (var dbContext = new BookContext())
{
    var author = dbContext.Authors.Add(new Author()
    {
        FullName = "Forexample"
    }).Entity;

    var page = dbContext.BookPages.Add(new BookPage()
    {
        Number = 1
    }).Entity;

    var book = new Book.Book()
    {
        Text = "new book"
    };

    book.Pages.Add(page);
    book.Author = author;

    dbContext.Books.Add(book);
    dbContext.SaveChanges();
}