@GineTik

Entity Framework Core не генерирует нужные таблицы, как решить?

Привет.

Почему-то не создаются несколько таблиц.

В решении у меня есть 2 проекта. Первый проект с asp.net core , а второй с entity framwork core.
В первый проект я добавил пакеты:
- Microsoft.EntityFrameworkCore.Design;
- Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.
Во второй:
- Microsoft.EntityFrameworkCore.Design;
- Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore;
- Microsoft.EntityFrameworkCore.SqlServer;
- Microsoft.EntityFrameworkCore.Tools;
- Microsoft.AspNetCore.Identity.EntityFrameworkCore.

Также создал класс ScheduleContext который наследует IdentityDbContext.
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using OnlineSchedule.Data.Entities;

namespace OnlineSchedule.Data.Contexts
{
    public class ScheduleContext : IdentityDbContext<User>
    {
        public DbSet<Schedule> Schedules;
        public DbSet<Day> Days;
        public DbSet<Lesson> Lessons;
        public DbSet<Comment> Comments;
        public DbSet<HistoryUserItem> HistoryUser;
        public DbSet<HistoryScheduleItem> HistorySchedule;
        public DbSet<TypeOfAction> TypeOfActions;
         
        public ScheduleContext(DbContextOptions<ScheduleContext> options) : base(options) { }
    }
}


Вот все сущности которые у меня есть:
using System.ComponentModel.DataAnnotations;

public class User : IdentityUser
{
    [Required]
    [MinLength(5), MaxLength(100)]
    public string Name { get; set; }
    public DateTime DateOfRegistration { get; set; }
    public bool IsRemoved { get; set; }
    public DateTime DateOfRemoving { get; set; }

    public List<Schedule> Schedules { get; set; }
}

public class BaseEntity<Tid>
{
    public Tid Id { get; set; }
}

public class Schedule : BaseEntity<Guid>
{
    [Required]
    [MinLength(5), MaxLength(100)]
    public string Title { get; set; }

    [MaxLength(3500)]
    public string About { get; set; }

    public bool CommentsIsAllow { get; set; }

    [Required]
    public DateTime DateOfCreation { get; set; }

    [Required]
    public int Position { get; set; }

    public int ComplaintsCount { get; set; }

    public List<Day> Days { get; set; }
    
    public List<Comment> Comments { get; set; }

    public string UserId { get; set; }
    public User User { get; set; }
}

public class Lesson : BaseEntity<Guid>
{
    [Required]
    [MinLength(5), MaxLength(255)]
    public string Title { get; set; }

    [Required]
    public DateTime DateOfCreation { get; set; }

    [Required]
    public int Position { get; set; }

    public Guid DayId { get; set; }
    public Day Day { get; set; }
}

public class HistoryUserItem : HistoryItem
{
    public string UserId { get; set; }
    public User User { get; set; }
}

public class HistoryScheduleItem : HistoryItem
{
    public string ScheduleId { get; set; }
    public Schedule Schedule { get; set; }
}

public class HistoryItem : BaseEntity<Guid>
{
    [Required]
    public string PerformerId { get; set; }
    public User Performer { get; set; }

    [Required]
    public DateTime DateOfAction { get; set; }

    [Required]
    public Guid TypeOfActionId { get; set; }
    public TypeOfAction TypeOfAction { get; set; }
}

public class Day : BaseEntity<Guid>
{
    [Required]
    [MinLength(5), MaxLength(255)]
    public string Title { get; set; }

    [Required]
    public DateTime DateOfCreation { get; set; }

    [Required]
    public int Position { get; set; }
    
    public Guid ScheduleId { get; set; }
    public Schedule Schedule { get; set; }

    public List<Lesson> Lessons { get; set; }
}

public class Comment : BaseEntity<Guid>
{
    //[Required]
    //[MaxLength(500)]
    public string Text { get; set; }

    //[Required]
    public DateTime DateOfCreation { get; set; }

    public int PositiveRating { get; set; }
    public int NegativeRating { get; set; }

    public bool IsFixed { get; set; }
    public int FixedPosition { get; set; }
    public bool IsRemoved { get; set; }
    public DateTime DateOfRemoved { get; set; }

    public string ScheduleId { get; set; }
    public Schedule Schedule { get; set; }
    public string UserId { get; set; }
    public User User { get; set; }
}


Сначала я думал что проблема в атрибутах (Required и тд.) но нет.
Как я заметил создается таблица Users и также все таблицы которые имеют косвенную связь с этой таблицей Users.

Вот бд которая сгенерировалась:
63063e0b321c6147708052.png

(Код миграции не влез из-за ограничения символов вопроса)

Если нужно больше информации то с радостью предоставлю :)
  • Вопрос задан
  • 275 просмотров
Решения вопроса 1
@GineTik Автор вопроса
Я просто в ScheduleContext прописал DbSet-ы как поля, а не свойства.
То есть я заменил
public DbSet<Schedule> Schedules;
public DbSet<Day> Days;
public DbSet<Lesson> Lessons;
public DbSet<Comment> Comments;
public DbSet<HistoryUserItem> HistoryUser;
public DbSet<HistoryScheduleItem> HistorySchedule;
public DbSet<TypeOfAction> TypeOfActions;
на
public DbSet<Schedule> Schedules { get; set; }
public DbSet<Day> Days { get; set; }
public DbSet<Lesson> Lessons { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<HistoryUserItem> HistoryUser { get; set; }
public DbSet<HistoryScheduleItem> HistorySchedule { get; set; }
public DbSet<TypeOfAction> TypeOfActions { get; set; }

В этом и была проблема :)
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 2
firedragon
@firedragon
Не джун-мидл-сеньор, а трус-балбес-бывалый.
@eudjien
Сначала нужно добавить миграцию dotnet ef migrations add НазваниеМиграции, а затем уже обновить БД на основе этой миграции.. dotnet ef database update
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы