在实体框架迁移中从复杂对象创建字段时出错

威利玛

我正在研究 Entity Framework 迁移到 .Net Core,当我执行迁移时出现错误。问题在于创建字段到复杂字段。

我有配置类,并在调试中执行代码。

配置类

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',当前数据库提供程序不支持该类型。使用“[NotMapped]”属性或在“OnModelCreating”中使用“EntityTypeBuilder.Ignore”更改属性 CLR 类型或忽略该属性

威利玛

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章