如何在连接 table 是架构一部分的 EF Core 中指定多对多关系?
How do I specify a many-to-many relationship in EF Core where the connection table is part of a schema?
在 https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key#joining-relationships-configuration 上,他们有以下示例说明如何在 Entity Framework 核心中指定多对多关系:
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
。
我所做的就是按照他们在页面上写的那样做 https://www.learnentityframeworkcore.com/configuration/many-to-many-relationship-configuration。在他们的示例中,他们有以下两个 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; }
}
然后他们做了以下 class 通过 many-to-many 关系将他们联系起来:
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);
}
在我的例子中,我这样配置连接 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
属于默认命名空间,RuleInstance
属于Compliance
命名空间,而这个连接tableAccountRule
也属于Compliance
命名空间,我可以通过语句 builder.ToTable("AccountRules", "Compliance");
.
配置
在 https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key#joining-relationships-configuration 上,他们有以下示例说明如何在 Entity Framework 核心中指定多对多关系:
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
。
我所做的就是按照他们在页面上写的那样做 https://www.learnentityframeworkcore.com/configuration/many-to-many-relationship-configuration。在他们的示例中,他们有以下两个 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; }
}
然后他们做了以下 class 通过 many-to-many 关系将他们联系起来:
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);
}
在我的例子中,我这样配置连接 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
属于默认命名空间,RuleInstance
属于Compliance
命名空间,而这个连接tableAccountRule
也属于Compliance
命名空间,我可以通过语句 builder.ToTable("AccountRules", "Compliance");
.