V
V
Victoria Kabishova2020-12-26 13:11:54
ASP.NET
Victoria Kabishova, 2020-12-26 13:11:54

How to add another model to a Razor Pages app in ASP.NET Core?

Hello, I want to add another model to my project, but when I try to do this command: "Add-Migration InitialCreate" an error occurs.

error screenshots

5fe705a8cf8ce259329455.png
5fe705baabad7302738035.png

I relied on this tutorial: https://docs.microsoft.com/en-us/aspnet/core/tutor... . I already made one model:

the code

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MyWebSIte.Models
{
    public class Company
    {
        public int ID { get; set; }

        [StringLength(60, MinimumLength = 3)]
        [Required]
        public string Title { get; set; }

        [Display(Name = "Release Date")]
        [DataType(DataType.Date)]
        public DateTime ReleaseDate { get; set; }

        [RegularExpression(@"^[A-Z]+[a-zA-Z0-9""'\s-]*$")]
        [StringLength(5)]
        [Required]
        public string Rating { get; set; }

        [RegularExpression(@"^[A-Z]+[a-zA-Z]*$")]
        [Required]
        [StringLength(30)]
        public string Bonus { get; set; }

        [RegularExpression(@"^[A-Z]+[a-zA-Z]*$")]
        [Required]
        [StringLength(30)]
        public string Description { get; set; }

        [RegularExpression(@"^[A-Z]+[a-zA-Z]*$")]
        [Required]
        [StringLength(30)]
        public string Thematics { get; set; }

        public byte[] Images { get; set; }

        [RegularExpression(@"^[A-Z]+[a-zA-Z]*$")]
        [Required]
        [StringLength(30)]
        public string Video { get; set; }

        [RegularExpression(@"^[A-Z]+[a-zA-Z]*$")]
        [Required]
        [StringLength(30)]
        public string Topic { get; set; }

        [RegularExpression(@"^[A-Z]+[a-zA-Z]*$")]
        [Required]
        [StringLength(30)]
        public string News { get; set; }

        [Range(1, 100)]
        [DataType(DataType.Currency)]
        [Column(TypeName = "decimal(18, 2)")]
        public decimal Price { get; set; }

        [RegularExpression(@"^[A-Z]+[a-zA-Z]*$")]
        [Required]
        [StringLength(30)]
        public string Tags { get; set; }
    }
}

Модель Company:
5fe709134ebb7568483645.png
Потом я ввела команду: Add-Migration InitialCreate, и вот что у меня создалось:
using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace MyWebSIte.Data.Migrations
{
    public partial class InitialCreate : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Company",
                columns: table => new
                {
                    ID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Title = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    ReleaseDate = table.Column<DateTime>(type: "datetime2", nullable: false),
                    Rating = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Bonus = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Description = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Thematics = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Images = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Video = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Topic = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    News = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Price = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
                    Tags = table.Column<string>(type: "nvarchar(max)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Company", x => x.ID);
                });
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Company");
        }
    }
}

А потом я создала 2 модель.
Модель данных:
using System;
using System.ComponentModel.DataAnnotations;

namespace MyWebSIte.Models
{
    public class Users
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public decimal Age { get; set; }
        public string Country { get; set; }
        public string City { get; set; }
        public string Email { get; set; }
        public string AboutMe { get; set; }
    }
}



But I can't create a migration, how can I do it?. how do i add another data model?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Victoria Kabishova, 2021-01-10
@Parsifal31017

https://docs.microsoft.com/en-us/aspnet/core/data/...

V
Vasily Bannikov, 2020-12-30
@vabka

You already have a Company table in your database (or not a table). Apparently they were there even before the migration.
Try this: https://docs.microsoft.com/en-us/ef/core/managing-...
then EF will scan your database for some bases and create models that reflect it.

V
Vladimir Korotenko, 2020-12-26
@firedragon

Keep the solution
https://stackoverflow.com/questions/43277154/entit...
At least I'm limited to int16 for age or is it planets?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question