2

I'm using Entity Framework 7 RC1, I have these entities:

public class Book
{
    [Key]
    public string BookId { get; set; }
    public List<BookPage> Pages { get; set; }
    public Author Author { get; set; }
    public string Text { get; set; }
} 

public class BookPage
{
    [Key]
    public string BookPageId { get; set; }
    public int Number { get; set; }
}

public class Author
{
    [Key]
    public string AuthorId { get; set; }
    public string FullName { get; set; }
}

ApplicationDbContext.cs is default, with public DbSet<Book> Books { get; set; }

When I'm trying to insert new Book, like this

var book = new Book()
{
    BookId = Guid.NewGuid().ToString(),
    Pages = new List<BookPage>()
    {
        new BookPage()
        {
            BookPageId = Guid.NewGuid().ToString(),
            Number = 1
        }
    },
    Author = new Author()
    {
        AuthorId = Guid.NewGuid().ToString(),
        FullName = "Forexample"
    },
    Text = "new book"
};
dbContext.Books.Add(book);
dbContext.SaveChanges();

It throws exception:

SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Book_Author_AuthorAuthorId".

What's the problem with this simple insert? I don't want manually add BookPage and Author, it's Entity Framework job.

Yurii N.
  • 5,455
  • 12
  • 42
  • 66

1 Answers1

2

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

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 :

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

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

And add your new entities like that :

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();
}
Community
  • 1
  • 1
Thomas
  • 24,234
  • 6
  • 81
  • 125