UWP SQL 服务器迁移不工作

UWP SQL Server migration not working

我安装了最新版本的 Windows 10(秋季创作者更新)和 Visual Studio 2017 (15.4)。

我创建了一个针对主要版本并由 Nuget 安装的 UWP 应用程序:

这是我的代码:

[Table("tbProva")]
public class Prova
{
    public Prova()
    {
        Indirizzi = new List<Indirizzo>();
    }

    [Key]
    public Guid Id { get; set; }

    [MaxLength(250)]
    public string Nome { get; set; }
    public ICollection<Indirizzo> Indirizzi { get; set; }
}

public class ProvaConfig : IEntityTypeConfiguration<Prova>
{
    public void Configure(EntityTypeBuilder<Prova> builder)
    {
        builder.HasMany(c => c.Indirizzi)
            .WithOne(c => c.Prova)
            .HasForeignKey(c => c.IdProva)
            .OnDelete(DeleteBehavior.Cascade);
    }
}

[Table("tbIndirizzi")]
public class Indirizzo
{
    [Key]
    public Guid Id { get; set; }
    [MaxLength(250)]
    public string Strada { get; set; }
    [MaxLength(10)]
    public string Civico { get; set; }
    public Prova Prova { get; set; }
    public Guid IdProva { get; set; }
}

public class DcContext : DbContext
{
    public DbSet<Prova> tbProva { get; set; }
    public DbSet<Indirizzo> tbIndirizzi { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server=Europa;Database=ProvaDb;Trusted_Connection=True;");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfiguration<Prova>(new ProvaConfig());
    }
}

如果我 运行 迁移,我得到这个结果:

PM> Add-Migration poi 

System.TypeLoadException: Non è stato possibile caricare il tipo 'System.Globalization.CultureInfo' dall'assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

如果我从模型中删除导航属性并且不覆盖 OnModelCreating,它工作正常。

谢谢

确保 System.Globalization NuGet 包已安装并且是最新的。

此外,如果您要使用 EntityFramework,请确保您也安装了 microsoft.entityframeworkcore 软件包。

这应该是一个已知问题,请参阅 issue #9666

While testing EF Core 2.0 with .NET UWP 6.0 we found that there are new types that present similar problems. E.g.: System.TypeLoadException: Could not load type 'System.Globalization.CultureInfo' from assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. System.TypeLoadException: Could not load type 'System.MarshalByRefObject' from assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

上述异常的一个例子是DataAnnotations。在您的代码片段中,刚刚引用了 System.ComponentModel.DataAnnotations 命名空间。

而且这个补丁错误似乎已被批准用于 2.0.x 补丁。参见 issue #9827