【问题标题】:How to set the composite primary key for owned entity types using entity framework core 3.1.1?如何使用实体框架核心 3.1.1 为拥有的实体类型设置复合主键?
【发布时间】:2020-03-04 15:15:15
【问题描述】:

我正在使用 asp.net core 3.1 和 efcore 3.1.1 和 Microsoft.EntityFrameworkCore.Cosmos 3.1.1 来开发 graphql API。

这是我的代码详细信息:

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 中用于集合的数据如下:文章

{
  "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”添加到类型“RelatedArticlesSchema.RelatedCountries#RelatedEntityId”,因为没有指定属性类型,也没有对应的 CLR 属性或字段。要添加影子状态属性,必须指定属性类型。

谁能帮我解决这个问题

【问题讨论】:

  • 正如错误所说,任何这些类中都没有 RelatedArticlesSchemaArticlesidid 属性。您应该在OnModelCreating 中分别配置每个实体,而不是尝试在另一个内部配置一个。即使语法似乎建议您可以嵌套和嵌套配置,它也不起作用

标签: c# azure-cosmosdb asp.net-core-3.1 ef-core-3.1


【解决方案1】:

你需要指定一个复合FK,因为主体有一个复合PK并首先声明shadow属性:

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 属性或字段。要添加影子状态属性,必须指定属性类型。\n
  • 哦,还需要指定复合外键,我更新了代码。
【解决方案2】:

您需要按如下方式更新您的 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

【讨论】:

  • Articles 上没有大部分导航。但是,是的,如果您不需要覆盖默认的影子 PK,这会更好
猜你喜欢
  • 1970-01-01
  • 2019-07-05
  • 2018-06-05
  • 2014-12-09
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
  • 2021-09-03
  • 2017-10-03
相关资源
最近更新 更多