c# - 如何使用实体框架核心 3.1.1 为拥有的实体类型设置复合主键?
问题描述
我正在使用 asp.net core 3.1 和 efcore 3.1.1 和 Microsoft.EntityFrameworkCore.Cosmos 3.1.1 来开发 graphql API。
这是我的代码详细信息:
文章.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; }
}
相关文章Schema.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 属性或字段。要添加影子状态属性,必须指定属性类型。
谁能帮我知道如何解决这个问题
解决方案
您需要指定复合 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);
});
});
推荐阅读
- mysql - 从表中获取使用相同映射表映射的 2 个字段的数据
- java - 如何从我的应用程序服务中退出另一个应用程序(不是我的)但不杀死它
- android - 构建失败并出现异常反应本机
- javascript - 如何找到多个焦点元素
- .htaccess - .htaccess 重定向到新域
- python - 给定python 3.5中模块的完整路径,如何导入模块?
- c++ - uniform_int_distribution <> 和有什么区别
? - javascript - 按键在我为某些聊天应用程序制作的功能中不起作用
- python - 如何打开与 Python 2 和 Python 3 兼容的 Tkinter askopenfilename 对话框
- node.js - 不断收到错误。找不到模块:错误:无法解析“文件路径”中的“@angular/animations”