1
public partial class Authors
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    public string Name { get; set; }        
    protected ICollection<Books> Books { get; set; }
}

Authors表有一个Guid主键,我签署了它的属性来生成数据库。

public class Books
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ISBNNumber { get; set; }
    public Guid AuthorId { get; set; }

    public virtual Authors Author { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        BookStoreEntities1 context = new BookStoreEntities1();

        Authors author = new Authors();
        author.Name = "Sample Author Name";

        Books authorBook1 = new Books();
        authorBook1.ISBNNumber = "11145-8854-9987";
        authorBook1.Name = "Book 1";

        Books authorBook2 = new Books();
        authorBook2.ISBNNumber = "4145-8854-2987";
        authorBook2.Name = "Book 2";

        author.AddBook(authorBook1);
        author.AddBook(authorBook2);

        context.Authors.Add(author);
        context.SaveChanges();
    }
}

我创建了客户端代码以将新记录插入数据库。此客户端代码将 author 插入Authors表,但不插入Books.

如果我更改Author Idint(not Guid),在这种情况下AuthorBooks插入数据库中。

你怎么看待这件事?

4

0 回答 0