[英]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.