HasNoKey在Add-Migrations中给出错误:“无法确定由导航属性...表示的关系”

问题描述

我有一个非常简单的架构。我正在尝试使用API​​。该API有2个类。 WeatherData类的嵌套类型为Coord。我想先做代码

public partial class WeatherData
    {
        [JsonProperty("coord")]
        public Coord Coord { get; set; }
    }

public partial class Coord
{
    [JsonProperty("lon")]
    public double Lon { get; set; }

    [JsonProperty("lat")]
    public double Lat { get; set; }
}

如您所见,它们都不具有主键。即使在以下代码中指定了hasNoKey(),然后输入Add-Migrations,我仍然收到错误Unable to determine the relationship represented by navigation property 'WeatherData.Coord' of type 'Coord'. Either manually configure the relationship,or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.,这是代码

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Coord>().HasNoKey();
    }

如果我指定关系:

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Coord>().HasNoKey();
        modelBuilder.Entity<WeatherData>(entity =>
        {
            entity.HasNoKey();
            entity.HasOne(e => e.Coord);
        });

    }

我收到错误消息:Object reference not set to an instance of an object.

请告诉我我在做什么错。我只需要2个无键类,并将它们存储在Azure sql DB中。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)