如何在使用Entity Framework Core 3.1.1时为拥有的实体类型设置复合主键?

4

我将使用asp.net core 3.1与efcore 3.1.1以及Microsoft.EntityFrameworkCore.Cosmos 3.1.1来开发GraphQL APIs。

下面是我的代码细节:

Articles.cs

public class Articles
{
    public string id { get; set; }

    public int ArticleId { get; set; }

    public string PublishedDate { get; set; }

    public ICollection<RelatedArticlesSchema> RelatedArticles { get; set; }

    public ICollection<RelatedEntityId> RelatedCountries { get; set; }
}

RelatedArticlesSchema.cs

public class RelatedArticlesSchema
{
    [JsonProperty("ArticleId")]
    public int ArticleId { get; set; }

    [JsonProperty("Title")]
    public string Title { get; set; }

    public ICollection<RelatedEntityId> RelatedCountries { get; set; }

    [JsonProperty("PartitionKey")]
    public string PublishedDate { get; set; }
}

RelatedEntityId.cs

public class RelatedEntityId
{
    public int IdVal { get; set; }
}

SampleDbContext.cs

public class SampleDbContext : DbContext
{
    public DbSet<Articles> Articles { get; set; }

    public DbSet<Countries> Countries { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        var converter = new NumberToStringConverter<int>();
        modelBuilder.Entity<Articles>().Property(e => e.LanguageId).HasConversion(converter);
        modelBuilder.Entity<Articles>().HasPartitionKey(o => o.LanguageId);
        modelBuilder.Entity<Articles>().OwnsMany(p => p.RelatedCountries, a =>
        {
            a.WithOwner().HasForeignKey("Articlesid");
            a.Property<int>("id");
            a.Property(o => o.IdVal);
        }

        ).OwnsMany(p => p.RelatedArticles, a =>
        {
            a.OwnsMany(p => p.RelatedCountries, a =>
            {
                a.WithOwner().HasForeignKey("RelatedArticlesSchemaArticlesidid");
                a.Property<int>("id");
                a.Property(o => o.IdVal);
            }

            );
            a.WithOwner().HasForeignKey("Articlesid");
            a.Property<int>("id");
            a.Property(o => o.ArticleId);
            a.Property(o => o.Title);
            a.Property(o => o.PublishedDate);
        }

        ).OwnsMany(p => p.RelatedResources, a =>
        {
            a.OwnsMany(p => p.RelatedCountries, a =>
            {
                a.WithOwner().HasForeignKey("RelatedArticlesSchemaArticlesidid");
                a.Property<int>("id");
                a.Property(o => o.IdVal);
            }

            );
            a.WithOwner().HasForeignKey("Articlesid");
            a.Property<int>("id");
            a.Property(o => o.ArticleId);
            a.Property(o => o.Title);
            a.Property(o => o.PublishedDate);
        }

        );
    }
}

以下是 CosmosDB 中集合 "Articles" 的数据。

{
  "ArticleId": 100,
  "PublishedDate": "12/1/2020 2:43:00 AM",
  "Author": null,
  "LanguageId": 37,
  "RelatedCountries": [
    {
      "IdVal": 1
    }
  ],
  "RelatedArticles": [
    {
      "ArticleId": 101,
      "Title": "Article_101",
      "RelatedCountries": [
        {
          "IdVal": 1
        }
      ],
      "PublishedDate": "5/26/2020 5:55:00 AM"
    },
    {
      "ArticleId": 102,
      "Title": "Article_102",
      "RelatedCountries": [
        {
          "IdVal": 1
        },
        {
          "IdVal": 2
        }
      ],
      "PublishedDate": "8/12/2020 4:57:00 AM"
    },
    {
      "ArticleId": 103,
      "Title": "Article_103",
      "RelatedCountries": [
        {
          "IdVal": 1
        },
        {
          "IdVal": 2
        },
        {
          "IdVal": 3
        }
      ],
      "PublishedDate": "8/20/2020 6:30:00 AM"
    },
    {
      "ArticleId": 104,
      "Title": "Article_104",
      "RelatedCountries": [
        {
          "IdVal": 10
        }
      ],
      "PublishedDate": "9/17/2020 6:06:00 AM"
    },
    {
      "ArticleId": 105,
      "Title": "Article_105",
      "RelatedCountries": [
        {
          "IdVal": 11
        }
      ],
      "PublishedDate": "11/26/2020 1:02:00 AM"
    }
  ]
}

我遇到了以下错误:

属性'RelatedArticlesSchemaArticlesidid'无法添加到'type 'RelatedArticlesSchema.RelatedCountries#RelatedEntityId''中,因为未指定属性类型且没有相应的CLR属性或字段。 要添加阴影状态属性,必须指定属性类型。

有人能帮我知道如何解决这个问题吗?

正如错误提示所说,在这些类中没有“RelatedArticlesSchemaArticlesidid”属性。你应该在 OnModelCreating 中单独配置每个实体,而不是尝试在其中一个内部进行配置。即使语法似乎暗示您可以嵌套和嵌套配置,但它并不起作用。 - Panagiotis Kanavos
我使用了这篇文章作为参考:https://learn.microsoft.com/en-us/ef/core/modeling/owned-entities - santosh kumar patro
2个回答

3
需要指定一个组合外键,因为主实体具有组合主键,首先声明影子属性:
modelBuilder.Entity<Articles>().Property(e => e.LanguageId).HasConversion(converter);
modelBuilder.Entity<Articles>().HasPartitionKey(o => o.LanguageId);
modelBuilder.Entity<Articles>()
    .OwnsMany(p => p.RelatedCountries, a =>
    {
        a.WithOwner().HasForeignKey("Articlesid");
        a.Property<int>("id");
        a.Property(o => o.IdVal);
    })
    .OwnsMany(p => p.RelatedArticles, a =>
    {
        a.WithOwner().HasForeignKey("Articlesid");
        a.Property<int>("id");
        a.Property(o => o.ArticleId);
        a.Property(o => o.Title);
        a.Property(o => o.PublishedDate);

        a.OwnsMany(p => p.RelatedCountries, a =>
        {
            a.Property<int>("RelatedArticlesSchemaArticlesidid");
            a.WithOwner().HasForeignKey("RelatedArticlesSchemaArticlesid", "RelatedArticlesSchemaArticlesidid");
            a.Property<int>("id");
            a.Property(o => o.IdVal);
        });
    })
    .OwnsMany(p => p.RelatedResources, a =>
    {
        a.WithOwner().HasForeignKey("Articlesid");
        a.Property<int>("id");
        a.Property(o => o.ArticleId);
        a.Property(o => o.Title);
        a.Property(o => o.PublishedDate);

        a.OwnsMany(p => p.RelatedCountries, a =>
        {
            a.Property<int>("RelatedArticlesSchemaArticlesidid");
            a.WithOwner().HasForeignKey("RelatedArticlesSchemaArticlesid", "RelatedArticlesSchemaArticlesidid");
            a.Property<int>("id");
            a.Property(o => o.IdVal);
        });
    });

感谢@Andriy Svyryd的回复。我尝试了第二个选项,但现在出现了一个错误:无法将属性'RelatedArticlesSchemaArticlesidid'添加到类型'RelatedArticlesSchema.RelatedCountries#RelatedEntityId'中,因为没有指定属性类型,也没有相应的CLR属性或字段。要添加阴影状态属性,必须指定属性类型。 - santosh kumar patro
哦,你还需要指定一个复合外键,我已经更新了代码。 - Andriy Svyryd

1
你需要按照以下方式更新你的dbcontext类:
        modelBuilder.Entity<Articles>().OwnsMany(p => p.RelatedCountries);
        modelBuilder.Entity<Articles>().OwnsMany(p => p.RelatedContacts);
        modelBuilder.Entity<Articles>().OwnsMany(p => p.RelatedCountryGroups);
        modelBuilder.Entity<Articles>().OwnsMany(p => p.RelatedTaxTags);
        modelBuilder.Entity<Articles>().OwnsMany(p => p.RelatedArticles, a =>
        {
            a.ToJsonProperty("RelatedArticles");
            a.OwnsMany(p => p.RelatedCountries);
        }

        );
        modelBuilder.Entity<Articles>().OwnsMany(p => p.RelatedResources, a =>
        {
            a.ToJsonProperty("RelatedArticles");
            a.OwnsMany(p => p.RelatedCountries);
        }

        );
        modelBuilder.Entity<Articles>().OwnsOne(p => p.Provisions);
        modelBuilder.Entity<Articles>().OwnsOne(p => p.ResourceGroup);
        modelBuilder.Entity<Articles>().OwnsOne(p => p.Disclaimer);

感谢 @AndriySvyryd 提供的文档: https://github.com/dotnet/efcore/commit/f70c3b62e49169f858c087d0cc22ee3edf307933#diff-4301fc6ef06589dd6e5e63debdd80f68L289


大多数这些导航在“文章”上都不存在。但是,如果您不需要覆盖默认的影子主键,则这样做会更好。 - Andriy Svyryd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接