简体   繁体   English

如何在连接表是架构一部分的 EF Core 中指定多对多关系?

[英]How do I specify a many-to-many relationship in EF Core where the connection table is part of a schema?

On https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key#joining-relationships-configuration they have the following example of how to specify a many-to-many relationship in Entity Framework Core:https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key#joining-relationships-configuration他们有以下内容如何在 Entity Framework Core 中指定多对多关系的示例:

modelBuilder.Entity<Post>()
    .HasMany(p => p.Tags)
    .WithMany(p => p.Posts)
    .UsingEntity<Dictionary<string, object>>(
        "PostTag",
        j => j
            .HasOne<Tag>()
            .WithMany()
            .HasForeignKey("TagId")
            .HasConstraintName("FK_PostTag_Tags_TagId")
            .OnDelete(DeleteBehavior.Cascade),
        j => j
            .HasOne<Post>()
            .WithMany()
            .HasForeignKey("PostId")
            .HasConstraintName("FK_PostTag_Posts_PostId")
            .OnDelete(DeleteBehavior.ClientCascade));

How do I do the same thing, but specify that PostTag is part of a schema other than the default one?我如何做同样的事情,但指定PostTag是模式的一部分而不是默认模式?

For instance, Test.PostTag and [Test].[PostTag] do not work.例如, Test.PostTag[Test].[PostTag]不起作用。 When trying to access the resource, it just results in the exception Microsoft.Data.SqlClient.SqlException: 'Invalid object name 'PostTag'.'尝试访问资源时,它只会导致异常Microsoft.Data.SqlClient.SqlException: 'Invalid object name 'PostTag'.' being thrown.被抛出。 So it seems to ignore the schema name Test when I try to specify it.因此,当我尝试指定模式名称Test时,它似乎忽略了它。

What I did was to do what they have written on the page https://www.learnentityframeworkcore.com/configuration/many-to-many-relationship-configuration .我所做的是按照他们在页面https://www.learnentityframeworkcore.com/configuration/many-to-many-relationship-configuration上写的内容进行操作。 In their example, they have the following two classes:在他们的示例中,他们有以下两个类:

public class Book
{
    public int BookId { get; set; }
    public string Title { get; set; }
    public Author Author { get; set; }
    public ICollection<BookCategory> BookCategories { get; set; }
}  
public class Category
{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
    public ICollection<BookCategory> BookCategories { get; set; }
}  

And then they made the following class to connect them through a many-to-many relationship:然后他们做了下面的 class 通过多对多的关系来连接他们:

public class BookCategory
{
    public int BookId { get; set; }
    public Book Book { get; set; }
    public int CategoryId { get; set; }
    public Category Category { get; set; }
}

And then they have the following configuration:然后他们有以下配置:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<BookCategory>()
        .HasKey(bc => new { bc.BookId, bc.CategoryId });  
    modelBuilder.Entity<BookCategory>()
        .HasOne(bc => bc.Book)
        .WithMany(b => b.BookCategories)
        .HasForeignKey(bc => bc.BookId);  
    modelBuilder.Entity<BookCategory>()
        .HasOne(bc => bc.Category)
        .WithMany(c => c.BookCategories)
        .HasForeignKey(bc => bc.CategoryId);
}

In my case, I configured the connection class like this:就我而言,我这样配置连接 class:

internal class AccountRuleEntityTypeConfiguration : IEntityTypeConfiguration<CompAccountRule>
{
    public void Configure(EntityTypeBuilder<AccountRule> builder)
    {
        builder
            .ToTable("AccountRules", "Compliance");
        builder
            .HasKey(x => new { x.AccountId, x.RuleInstanceId });
        builder
            .HasOne(x => x.Account)
            .WithMany(x => x.AccountRules)
            .HasForeignKey(x => x.AccountId);
        builder
            .HasOne(x => x.RuleInstance)
            .WithMany(x => x.AccountRules)
            .HasForeignKey(x => x.RuleInstanceId);
    }
}

Account belongs to the default namespace, RuleInstance belongs to the Compliance namespace, and this connection table AccountRule also belongs to the Compliance namespace, which is I'm able to configure through the statement builder.ToTable("AccountRules", "Compliance"); Account属于 default 命名空间, RuleInstance属于Compliance命名空间,而这个连接表AccountRule也属于Compliance命名空间,这是我可以通过语句builder.ToTable("AccountRules", "Compliance"); . .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何直接填充 EF Core 为多对多关系生成的连接表? - How can I directly fill join table generated by EF Core for many-to-many relationship? 如何在EF Core多对多关系中为删除建模? - How do I model deletion in an EF Core many-to-many relationship? ef核心-如何将表从一种模式复制到具有多对多关系的另一种模式? - ef core - how to copy tables from one schema to other with many-to-many relationship? 如何使用自动映射器将具有多对多关系的 DTO 映射到具有关系表的 EF 核心实体? - How to map a DTO with a many-to-many relationship to a EF core entity with a relationship table using automapper? EF Core 3.1 如何在多对多关系中自动映射 - EF Core 3.1 how to automap in Many-to-many relationship EF Core如何选择具有多对多关系的实体 - EF Core how select entity with many-to-many relationship 如何修改EF Core 5中多对多实体关系中生成的连接表 - How to modify the join table generated in many-to-many entity relationship in EF Core 5 我将如何在 EF Core 中创建这种多对多关系? - How would I go about creating this many-to-many relationship in EF Core? 多对多关系和唯一约束。 如何在EF Core中做到这一点? - Many-to-many relation and unique constraint. How do I do that in EF Core? EF Core-如何为已经存在一对多关系的同一实体设置多对多 - EF Core - How to setup many-to-many for same entity that already has a one-to-many relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM