与EF Code First和多个架构的多对多关系

时间:2019-03-26 11:36:39

标签: c# entity-framework-6 ef-code-first mapping entity-relationship

我正在尝试使用多个架构创建数据库结构。现在,我需要将User表(身份验证架构)链接到TrafficArea表(资产架构)。但是,在生成迁移时,它将尝试在资产架构中创建User表。

到目前为止,我已经设置了以下内容

AssetsContext.cs-OnModelCreating()

modelBuilder.HasDefaultSchema("assets");

modelBuilder.Entity<TrafficArea>()
.HasMany(tm => tm.TransportManagers) // This is ICollection<User>
.WithMany(u => u.TrafficAreas)
.Map(tatm =>
{
    tatm.MapLeftKey("TrafficAreaId");
    tatm.MapRightKey("UserId");
    tatm.ToTable("TrafficAreaTransportManagers", "assets");
});

AuthenticationContext.cs-OnModelCreating()

modelBuilder.HasDefaultSchema("authentication");

运行迁移时,我得到以下尝试运行

CreateTable(
"assets.User",
c => new
{
    Id = c.Guid(nullable: false),
    Email = c.String(),
    etc...
})
.PrimaryKey(t => t.Id);

CreateTable(
"assets.TrafficAreaTransportManagers",
c => new
{
    TrafficAreaId = c.Guid(nullable: false),
    UserId = c.Guid(nullable: false),
})
.PrimaryKey(t => new { t.TrafficAreaId, t.UserId })
.ForeignKey("assets.TrafficArea", t => t.TrafficAreaId, cascadeDelete: true)
.ForeignKey("assets.User", t => t.UserId, cascadeDelete: true)
.Index(t => t.TrafficAreaId)
.Index(t => t.UserId);

如何确保未在资产架构上重新创建User表。

我试图手动更改迁移脚本以确保它指向身份验证。用户表如下所示:

CreateTable(
"assets.TrafficAreaTransportManagers",
c => new
{
    TrafficAreaId = c.Guid(nullable: false),
    UserId = c.Guid(nullable: false),
})
.PrimaryKey(t => new { t.TrafficAreaId, t.UserId })
.ForeignKey("assets.TrafficArea", t => t.TrafficAreaId, cascadeDelete: true)
.Index(t => t.TrafficAreaId)
.Index(t => t.UserId);

AddForeignKey("assets.TrafficAreaTransportManagers", "UserId", "authentication.User", "Id");

但是在运行时出现此错误:

enter image description here

0 个答案:

没有答案