I have Tag
and Post
classes (many-to-many relation, code-first)
class Post {
ICollection<Tag> Tags {get; set;} = new List<Tag>();
}
class Tag {
ICollection<Post> Posts {get; set;} = new List<Post>();
}
Tag
and Post
classes has corresponding Tag
and Post
tables with TagId
and PostId
primary keys (note the singular naming convention)
When configuring my Post
's builder I say
builder
.HasMany(p => p.Tags)
.WithMany(p => p.Posts)
.UsingEntity(j => j.HasData(new { PostId = 1, TagId = 1 }));
When I add try to add-migration Initial
a blocking error interrupts the migration creation:
The seed entity for entity type 'PostTag (Dictionary<string, object>)' cannot be added because no value was provided for the required property 'PostsId'.
Could someone explain what it hidden on behind this message?
I see it passes with the plurals (PostsId
vs PostId
), but I don't want to use the plural on the foreignkeys, is there a way to use PostId
instead of PostsId
?
.UsingEntity(j => j.HasData(new { PostsId = 1, TagsId = 1 }));
Also, it creates the following migration:
migrationBuilder.CreateTable(
name: "PostTag (Dictionary<string, object>)",
columns: table => new
{
PostsId = table.Column<int>(type: "int", nullable: false),
TagsId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_PostTag (Dictionary<string, object>)", x => new { x.PostsId, x.TagsId });
table.ForeignKey(
name: "FK_PostTag (Dictionary<string, object>)_Post_PostsId",
column: x => x.PostsId,
principalTable: "Post",
principalColumn: "PostId",
onDelete: ReferentialAction.Cascade);
The table name "PostTag (Dictionary<string, object>)" (what a horror) and also PrimaryKey("PK_PostTag (Dictionary<string, object>)" ...
What is the logic to use such conventions ? What to do to have the normal PostTag
table name?