如何将实体Id分配给另一个实体,而不将第一个实体插入到实体框架c#中的数据库中?

时间:2021-07-25 09:39:52

Suppose I have 2 tables with many to one relationship: Book -> Author. Author can have multiple books. So, Book entity has Author and AuthorId navigation properties.

假设我有两个表,它们之间的关系是:Book ->作者。作者可以有多本书。因此,Book实体具有作者和授权导航属性。

For example Author table contains one row: AuthorName. I want to insert new Author only if the name is unique.

例如,Author表包含一行:AuthorName。我想要插入新的作者,只有当名称是唯一的。

So, what I do:

所以,我所做的:

var book = new Book();
var authorFromDatabase = getAuthorFromDbByName("author name");
if(authorFromDatabase == null)
{
    // insert new author if it is not already in the database
    book.Author = new Author("author name");
}
else
{
    // how to assign AuthorId to book so that it will not add new Author to the db??
    // the following line inserts new author into the db
    book.AuthorId = authorFromDatabase .AuthorId;
}

So, how can I assign AuthorId to book and not insert a new Author into the db if it's already there?

那么,如果db已经存在,我如何分配AuthorId来预订,而不将新的作者插入到db中呢?

2 个解决方案

#1


2  

Setting the .AuthorId property would not create a new Author in the DB - how could it? You're not actually constructing a new Author object and adding it to your DbContext. From what you've given us to look at, it would seem if(authorFromDatabase == null) always resolves as True. Have you debugged the code to ensure that the else block is ever executed?

设置.AuthorId属性不会在DB中创建新作者——怎么会这样呢?实际上,您并没有构造一个新的Author对象并将其添加到DbContext中。从您给我们的内容来看,似乎(authorFromDatabase == null)总是可以解析为True。您是否调试了代码以确保执行了else块?

You should probably show more of of your code, such as your Author and Book entity classes as well as your getAuthorFromDbByName(...) method implementation and where you instantiate your DbContext instances.

您可能应该展示更多的代码,例如作者和Book实体类,以及getAuthorFromDbByName(…)方法实现,以及实例化DbContext实例的地方。

#2


0  

If it is about uniqueness I would do something like this:

如果是关于独特性,我会这样做:

// some code
newBook.Author = CreateOrGetAuthor("The Name");
// more code

private Author CreateOrGetAuthor(string authorName)
{
    var author = _context.Authors.SingleOrDefault(a => a.Name.Equals(authorName, StringComparison.CurrentCultureIgnoreCase));
    if (author == null)
    {
        author = new Author();
        author.Name = authorName;
    }
    return author; 
}

Now it depends on if you're using transactions around those both calls. If not, you have to call _context.SaveChanges() first and the return the author in CreateOrGetAuthor. Otherwise it won't have an ID.

现在它取决于你是否在这两个调用周围使用事务。如果不是,则必须首先调用_context.SaveChanges(),并返回CreateOrGetAuthor中的author。否则它就没有ID了。

#1


2  

Setting the .AuthorId property would not create a new Author in the DB - how could it? You're not actually constructing a new Author object and adding it to your DbContext. From what you've given us to look at, it would seem if(authorFromDatabase == null) always resolves as True. Have you debugged the code to ensure that the else block is ever executed?

设置.AuthorId属性不会在DB中创建新作者——怎么会这样呢?实际上,您并没有构造一个新的Author对象并将其添加到DbContext中。从您给我们的内容来看,似乎(authorFromDatabase == null)总是可以解析为True。您是否调试了代码以确保执行了else块?

You should probably show more of of your code, such as your Author and Book entity classes as well as your getAuthorFromDbByName(...) method implementation and where you instantiate your DbContext instances.

您可能应该展示更多的代码,例如作者和Book实体类,以及getAuthorFromDbByName(…)方法实现,以及实例化DbContext实例的地方。

#2


0  

If it is about uniqueness I would do something like this:

如果是关于独特性,我会这样做:

// some code
newBook.Author = CreateOrGetAuthor("The Name");
// more code

private Author CreateOrGetAuthor(string authorName)
{
    var author = _context.Authors.SingleOrDefault(a => a.Name.Equals(authorName, StringComparison.CurrentCultureIgnoreCase));
    if (author == null)
    {
        author = new Author();
        author.Name = authorName;
    }
    return author; 
}

Now it depends on if you're using transactions around those both calls. If not, you have to call _context.SaveChanges() first and the return the author in CreateOrGetAuthor. Otherwise it won't have an ID.

现在它取决于你是否在这两个调用周围使用事务。如果不是,则必须首先调用_context.SaveChanges(),并返回CreateOrGetAuthor中的author。否则它就没有ID了。