Entity Framework Map() property alternate in EF Core 6

huangapple go评论41阅读模式
英文:

Entity Framework Map() property alternate in EF Core 6

问题

因为在EF Core中不再支持Map()属性,所以您需要修改代码以适应EF Core 6。以下是修改后的代码示例:

modelBuilder.Entity<tblStoreGroup>()
    .HasMany(e => e.tblUsers)
    .WithMany(e => e.tblStoreGroups)
    .UsingEntity(j => j.ToTable("tblBridgeUserGroup")
                        .Property("GroupID", typeof(int))
                        .Property("username", typeof(string)));

这将帮助您在EF Core 6中实现相同的关联关系。如果您对Entity Framework有任何疑问,请随时提问。

英文:

I am migrating my project from Entity Framework to EF Core 6 but now I am stuck at this point.

modelBuilder.Entity&lt;tblStoreGroup&gt;()
            .HasMany(e =&gt; e.tblUsers)
            .WithMany(e =&gt; e.tblStoreGroups)
            .Map(m =&gt; m.ToTable(&quot;tblBridgeUserGroup&quot;)
                       .MapLeftKey(&quot;GroupID&quot;)
                       .MapRightKey(&quot;username&quot;));

Because the Map() property is not available in EF Core, can someone please provide a solution for this code?

I am a beginner in Entity Framework.

Thanks in advance

答案1

得分: 1

你可以尝试使用 UsingEntity

modelBuilder.Entity&lt;tblStoreGroup&gt;()
    .HasMany(p =&gt; p.tblUsers)
    .WithMany(p =&gt; p.tblStoreGroups)
    .UsingEntity&lt;Dictionary&lt;string, object&gt;&gt;(
        "tblBridgeUserGroup",
        j =&gt; j
            .HasOne&lt;tblUser&gt;()
            .WithMany()
            .HasForeignKey("username"),
        j =&gt; j
            .HasOne&lt;tblStoreGroup&gt;()
            .WithMany()
            .HasForeignKey("GroupID"));

请参阅关系配置中的连接部分,查看文档。

英文:

You can try using UsingEntity:

modelBuilder.Entity&lt;tblStoreGroup&gt;()
	.HasMany(p =&gt; p.tblUsers)
	.WithMany(p =&gt; p.tblStoreGroups)
	.UsingEntity&lt;Dictionary&lt;string, object&gt;&gt;(
		&quot;tblBridgeUserGroup&quot;,
		j =&gt; j
			.HasOne&lt;tblUser&gt;()
			.WithMany()
			.HasForeignKey(&quot;username&quot;),
		j =&gt; j
			.HasOne&lt;tblStoreGroup&gt;()
			.WithMany()
			.HasForeignKey(&quot;GroupID&quot;));

See Joining relationships configuration section of the docs.

huangapple
  • 本文由 发表于 2023年2月18日 02:58:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定