Ошибка создания поля из сложного объекта в миграции Entity Framework - PullRequest
0 голосов
/ 05 апреля 2019

Я изучаю миграцию Entity Framework на .Net Core, и когда я выполню миграцию, я получаю сообщение об ошибке.Проблема в создании поля в сложное поле.

У меня есть класс config и при отладке код выполняется.

Класс конфигурации

public void Configure(EntityTypeBuilder<City> builder)
{
    base.Configure(builder);

    builder.Property(x => x.Name)
        .IsRequired()
        .HasColumnType(VARCHAR)
        .HasMaxLength(150);
    builder.Property(x => x.Initials)
        .HasColumnType(VARCHAR)
        .HasMaxLength(5);
    builder.Property(x => x.Code)
        .HasColumnType(VARCHAR)
        .HasMaxLength(5);
    builder.Property(x => x.State)
        .HasColumnType(UNIQUEIDENTIFIER)
        .IsRequired();            
    }

Мой контекстный класс

public class DataContext: DbContext
{
    protected virtual DbSet<Country> Country { get; set; }
    protected virtual DbSet<State> State { get; set; }
    protected virtual DbSet<City> City { get; set; }

    public DataContext()
    { }

    public DataContext(DbContextOptions<DataContext> opcoes)
        :base(opcoes)
    { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ForSqlServerUseIdentityColumns();
        modelBuilder.HasDefaultSchema("MCDATA");

        new CountryConfig().Configure(modelBuilder.Entity<Country>());
        new StateConfig().Configure(modelBuilder.Entity<State>());
        new CityConfig().Configure(modelBuilder.Entity<City>());
    }
}

public class BaseEntity
{
    public Guid Id { get; set; }
    public DateTime RegisterDate { get; set; }
    public DateTime LastChangeDate { get; set; }
    public Status Status { get; set; }
}

public class City: BaseEntity, IEquatable<City>
{
    public string Name { get; set; }
    public string Initials { get; set; }
    public string Code { get; set; }
    public State State { get; set; }

    public bool Equals(City other)
    {
        throw new NotImplementedException();
    }
}

public class State: BaseEntity, IEquatable<State>
{
    public string Name { get; set; }
    public string Code { get; set; }
    public string Initials { get; set; }
    public Country Country { get; set; }

    public bool Equals(State other)
    {
        throw new NotImplementedException();
    }
}

public DataContext CreateDbContext(string[] args)
{
    var construtor = new DbContextOptionsBuilder<DataContext>();
    construtor.UseSqlServer(CONNECTIONSTRING);
    return new DataContext(construtor.Options);
}

Проблема в context.Database.Migrate();

var context = dbFactory.CreateDbContext(new string[] {});
context.Database.Migrate();

Сообщение:

System.InvalidOperationException: 'Свойство' City.State 'имеет тип 'State', который не поддерживается текущим поставщиком базы данных.Измените тип CLR свойства или игнорируйте свойство, используя атрибут «[NotMapped]» или «EntityTypeBuilder.Ignore» в «OnModelCreating»

1 Ответ

0 голосов
/ 05 апреля 2019

В классе CityConfig измените код:

    public override void Configure(EntityTypeBuilder<City> builder)
    {
        base.Configure(builder);

        builder.Property(x => x.Name)
            .IsRequired()
            .HasColumnType(VARCHAR)
            .HasMaxLength(150);
        builder.Property(x => x.Initials)
            .HasColumnType(VARCHAR)
            .HasMaxLength(5);
        builder.Property(x => x.Code)
            .HasColumnType(VARCHAR)
            .HasMaxLength(5);
        // Remove this
        //builder.Property(x => x.State)
        //    .HasColumnType(UNIQUEIDENTIFIER)
        //    .IsRequired();

        // Add this
        builder
            .HasOne(y => y.State)
            .WithMany();
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...