简体   繁体   English

我无法在Entity Framework Core中播种数据

[英]I cannot seed data in Entity Framework Core

I cannot seed data in my webapp and don't know what's the problem. 我无法将数据植入我的Webapp中,也不知道出了什么问题。 The app works fine and the user can add data to the database. 该应用程序运行良好,用户可以将数据添加到数据库中。 I have added the saveChanges method but no results. 我添加了saveChanges方法,但没有结果。 I think I'm sending null data to the database but I can't find the problem. 我想我正在将空数据发送到数据库,但找不到问题。 My files: DbContext 我的文件:DbContext

 public class BookDbContext: DbContext
{
    public BookDbContext(DbContextOptions options): base(options)
    {

    }

    public DbSet<Books> Books { get; set; }
}

SeedBooks SeedBooks

public static void Initialize(BookDbContext context)
    {
        context.Database.EnsureCreated();

        if (context.Books.Any())
        {
            return;
        }

        var books = new Books[]
         {
        new Books{Id=1, Title ="Kont of Mount Cristos", Description="About eghteen century", Author="Don Mon", Category=Category.aksion},
        new Books{Id=2, Title="Introduction in C Sharp", Description="Learning computer programing", Author="Scott Allen", Category= Category.education},
        new Books{Id=3, Title="The Castle", Description="Learning about history", Author="Ismail Kadare", Category= Category.drama},
        new Books{Id=4, Title="Madam Bavary", Description="Love story", Author="Gi de Mopasan", Category= Category.romance}

         };

        foreach (Books b in books)
        {
            context.Books.Add(b);
        }
            context.SaveChanges();

    }

Books file 书籍档案

public enum Category
{
    aksion, drama, romance, education
}
public class Books
{
    public int Id { get; set; }
    [Required]
    public string Title { get; set; }
    [Required]
    [Display(Name = "Autor Name")]
    public string Author { get; set; }
    [Required]
    [MaxLength(80)]
    public string Description { get; set; }
    [Required]
    public Category Category { get; set; }






}

Index file for the Books 书籍的索引文件

 <table class="table">
    @*<thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => Model.Author)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Title)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Category)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Description)
                </th>
                <th></th>
            </tr>
        </thead>*@
    <tbody>
        @foreach (var item in Model.Book)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Author)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Title)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Category)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Description)
                </td>
                <td>
                    <a asp-action="Details" asp-route-id="@item.Id">Details</a>

                </td>
            </tr>
        }
    </tbody>
</table>

<a asp-action="Create" asp-controller="Books">Create</a>

I reproduced your problem with asp.net core mvc with ef core tutorial . 我用ef core 教程转载了asp.net core mvc的问题。

I followed the article and found when you run the project and it will create a database in local and create a student table which contains the data you have initialize. 我阅读了这篇文章,发现在运行项目时,它将在本地创建一个数据库,并创建一个学生表,其中包含您已初始化的数据。

var context = services.GetRequiredService<SchoolContext>();
DbInitializer.Initialize(context);

However, I found that the table design is as follow: 但是,我发现表设计如下:

CREATE TABLE [dbo].[Student] (
    [ID]       INT            IDENTITY (1, 1) NOT NULL,
    [Age]      INT            NOT NULL,
    [LastName] NVARCHAR (MAX) NULL,
    CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED ([ID] ASC)
);

The ID property has set IDENTITY (1, 1) , which means that you need to add ID when you want to add every data into table. ID属性设置了IDENTITY(1,1) ,这表示要将每个数据添加到表中时都需要添加ID。

So delete "IDENTITY(1,1)" and it will work fine. 因此, 删除“ IDENTITY(1,1)” ,它将正常工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM