EF Core 错误加入实体类型名称
EF Core wrong Join entity type name
我有三个实体,如下所示:
public class Application
{
[Key]
public int ApplicationId { get; set; }
public string Name { get; set; }
public virtual ICollection<User> Users { get; set; }
}
public class User
{
[Key]
public int UserId { get; set; }
public string UserName { get; set; }
public virtual ICollection<Application> Applications { get; set; }
}
加入实体
public class UserApplication
{
public int UserId { get; set; }
public int ApplicationId { get; set; }
[ForeignKey("UserId")]
public User User { get; set; }
[ForeignKey("ApplicationId")]
public Application Application { get; set; }
}
OnModelCreating 节 =>
modelBuilder.Entity<User>()
.HasMany(x => x.Applications)
.WithMany(x => x.Users)
.UsingEntity(ua => ua.ToTable("UserApplication"));
modelBuilder.Entity<UserApplication>()
.HasKey(a=> new { a.ApplicationId, a.UserId});
运行 代码导致错误
invalid object name => ApplicationUser.
注意 - 虽然 OnModelCreating
只有名称错误的实体存在。数据库具有 table 名称 UserApplication
您混合使用了显式和隐式连接实体。恐怕 EF Core 假设 2 个单独的 many-to-many 关系与 2 个单独的表。请注意,按照惯例,隐式连接实体名称为 {Name1}{Name2}
,名称按升序排列,在您的情况下为 ApplicationUser
.
您需要的是使用 UsingEntity
流畅 API 的泛型重载,并将显式连接实体类型作为泛型类型参数传递。还要在那里配置加入实体,而不是单独配置。例如
modelBuilder.Entity<User>()
.HasMany(x => x.Applications)
.WithMany(x => x.Users)
.UsingEntity<UserApplication>(
// ^^^
ua => ua.HasOne(e => e.Application).WithMany().HasForeignKey(e => e.ApplicationId),
ua => ua.HasOne(e => e.User).WithMany().HasForeignKey(e => e.UserId),
ua =>
{
ua.ToTable("UserApplication");
ua.HasKey(a => new { a.ApplicationId, a.UserId });
});
我有三个实体,如下所示:
public class Application
{
[Key]
public int ApplicationId { get; set; }
public string Name { get; set; }
public virtual ICollection<User> Users { get; set; }
}
public class User
{
[Key]
public int UserId { get; set; }
public string UserName { get; set; }
public virtual ICollection<Application> Applications { get; set; }
}
加入实体
public class UserApplication
{
public int UserId { get; set; }
public int ApplicationId { get; set; }
[ForeignKey("UserId")]
public User User { get; set; }
[ForeignKey("ApplicationId")]
public Application Application { get; set; }
}
OnModelCreating 节 =>
modelBuilder.Entity<User>()
.HasMany(x => x.Applications)
.WithMany(x => x.Users)
.UsingEntity(ua => ua.ToTable("UserApplication"));
modelBuilder.Entity<UserApplication>()
.HasKey(a=> new { a.ApplicationId, a.UserId});
运行 代码导致错误
invalid object name => ApplicationUser.
注意 - 虽然 OnModelCreating
只有名称错误的实体存在。数据库具有 table 名称 UserApplication
您混合使用了显式和隐式连接实体。恐怕 EF Core 假设 2 个单独的 many-to-many 关系与 2 个单独的表。请注意,按照惯例,隐式连接实体名称为 {Name1}{Name2}
,名称按升序排列,在您的情况下为 ApplicationUser
.
您需要的是使用 UsingEntity
流畅 API 的泛型重载,并将显式连接实体类型作为泛型类型参数传递。还要在那里配置加入实体,而不是单独配置。例如
modelBuilder.Entity<User>()
.HasMany(x => x.Applications)
.WithMany(x => x.Users)
.UsingEntity<UserApplication>(
// ^^^
ua => ua.HasOne(e => e.Application).WithMany().HasForeignKey(e => e.ApplicationId),
ua => ua.HasOne(e => e.User).WithMany().HasForeignKey(e => e.UserId),
ua =>
{
ua.ToTable("UserApplication");
ua.HasKey(a => new { a.ApplicationId, a.UserId });
});