代码之家  ›  专栏  ›  技术社区  ›  Daniel Jonsson

如果连接表是模式的一部分,如何在EF Core中指定多对多关系?

  •  0
  • Daniel Jonsson  · 技术社区  · 2 年前

    在…上 https://docs.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));
    

    我该如何做同样的事情,但要具体说明这一点 PostTag 架构的一部分不是默认的吗?

    例如, Test.PostTag [Test].[PostTag] 不要工作。尝试访问资源时,只会导致异常 Microsoft.Data.SqlClient.SqlException: 'Invalid object name 'PostTag'.' 被扔了。所以它似乎忽略了模式名 Test 当我试图指定它时。

    0 回复  |  直到 2 年前
        1
  •  0
  •   Daniel Jonsson    2 年前

    我所做的就是按照他们在页面上写的做 https://www.learnentityframeworkcore.com/configuration/many-to-many-relationship-configuration .在他们的例子中,他们有以下两类:

    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; }
    }  
    

    然后他们制作了以下课程,通过多对多关系将他们联系起来:

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

    然后它们有以下配置:

    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);
    }
    

    在我的例子中,我将连接类配置为:

    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 属于默认名称空间, RuleInstance 属于 Compliance 名称空间,以及这个连接表 AccountRule 也属于 顺从 名称空间,我可以通过语句来配置它 builder.ToTable("AccountRules", "Compliance"); .