微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。