Error to create field from the complex object in Entity Framework migration

我只是一个虾纸丫 提交于 2019-12-11 17:21:44

问题


I am studying Entity Framework migration to .Net Core and when I will execute the migration I get an error. The problem is in create field to a complex field.

I have the config class and in debug the code is executed.

Config class

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();            
    }

My context class

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);
}

The problem is in context.Database.Migrate();

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

The message is:

System.InvalidOperationException: 'The property 'City.State' is of type 'State' which is not supported by current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'


回答1:


In the CityConfig class change the code:

    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();
    }


来源:https://stackoverflow.com/questions/55525321/error-to-create-field-from-the-complex-object-in-entity-framework-migration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!