简体   繁体   English

当我们在ASP.NET MVC中播种数据库时,为什么我们使用lamba表达式返回元素的id?

[英]When we Seed a database in ASP.NET MVC, why do we use a lamba expression for returning the id of elements?

In all the examples I see, such as 在我看到的所有示例中,例如

protected override void Seed(BookService.Models.BookServiceContext context)
{
    context.Authors.AddOrUpdate(x => x.Id,
        new Author() { Id = 1, Name = "Jane Austen" },
        new Author() { Id = 2, Name = "Charles Dickens" },
        new Author() { Id = 3, Name = "Miguel de Cervantes" }
        );

    context.Books.AddOrUpdate(x => x.Id,
        new Book() { Id = 1, Title = "Pride and Prejudice", Year = 1813, AuthorId = 1, 
            Price = 9.99M, Genre = "Comedy of manners" },
        new Book() { Id = 2, Title = "Northanger Abbey", Year = 1817, AuthorId = 1, 
            Price = 12.95M, Genre = "Gothic parody" },
        new Book() { Id = 3, Title = "David Copperfield", Year = 1850, AuthorId = 2, 
            Price = 15, Genre = "Bildungsroman" },
        new Book() { Id = 4, Title = "Don Quixote", Year = 1617, AuthorId = 3, 
            Price = 8.95M, Genre = "Picaresque" }
        );
}

from here , I see that there is a lamba expression as the first parameter of the AddOrUpdate and it's always shown as the Id (primary key?) of the table. 这里 ,我看到有一个兰巴表达式作为第一个参数AddOrUpdate它总是显示为Id (主键?)的表。 Can someone explain what that lamba expression is for? 有人可以解释一下该兰巴表达的目的吗? Is it for checking whether the Id is null ? 是否用于检查Id是否为null I'm confused. 我糊涂了。

It's to define which property should be checked to see if the Seed() -method should UPDATE or INSERT . 它定义了应该检查哪个属性,以查看Seed()方法是否应该UPDATEINSERT In this case, if the given ID is already in the database, it will not insert it again (but update the existing entry). 在这种情况下,如果给定的ID已存在于数据库中,则不会再次插入它(而是更新现有条目)。

But you could also check on Title , Year , ... or other properties to define if the entry should be inserted or updated in your database. 但是,您也可以检查TitleYear ,...或其他属性,以定义是否应在数据库中插入或更新条目。 Basically it's to prevent duplicates in your database, but you can give meaning to what a duplicate is to you. 基本上是为了防止数据库中出现重复项,但是您可以赋予重复项含义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我们需要在ASP.NET MVC中调用父类的空虚方法 - Why do we need to call empty virtual method of parent class in ASP.NET MVC 何时以及为何在asp.net mvc 2中使用TryUpdateModel? - When and why do you use TryUpdateModel in asp.net mvc 2? 我们何时需要在ASP.NET MVC中实现自定义TypeConverter? - When do we need to implement a custom TypeConverter in ASP.NET MVC? 如果我们在asp.net mvc 4 default示例中更改默认数据库名称,应该在哪里更改? - If we change default database name in asp.net mvc 4 default example , what are the places we should change? 为什么我们在ASP.NET中使用.class文件? - Why we use .class file in ASP.NET? 在 MVC5 ASP.Net 应用程序中使用 Repository 和 UnitOfWork 模式时,如何修改数据库初始值设定项 Seed 方法? - How do modify database initializer Seed method when using Repository and UnitOfWork patterns in MVC5 ASP.Net application? ASP.NET 核心:为什么我们需要 IDesignTimeDbContextFactory? - ASP.NET Core : Why do we need IDesignTimeDbContextFactory? 在ASP.NET MVC Razor View中合并包含lamba表达式的输入元素 - Consolidating input elements containing lamba expressions in ASP.NET MVC Razor View ASP.NET Core 3.1 MVC 中的种子数据库 AspNetUserRoles 表 - Seed database AspNetUserRoles table in ASP.NET Core 3.1 MVC Asp.Net MVC4 Web API-我们是否需要OData来构建快速查询服务 - Asp.Net MVC4 Web API - Do we need OData for building a fast query service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM