ASP.NET 迁移正在创建重复的桥接实体
ASP.NET migrations is creating duplicate bridge entities
我正在使用 MVC 5 和 EF 6 设计一个 ASP.NET 的 Web 应用程序。我们的团队使用代码优先迁移来设计我们的数据库。在我们的项目中,我们有两个模型 - Location
和 Recreation
。 Recreation 本质上是一个可枚举的标签,可以应用于任何 Location(多对多关系),所以我设计了一个 3rd bridge 实体模型来处理关系(所以我可以在代码中引用它)。以下是缩写的模型定义:
地点:
/*
* Represents a location on a map.
*/
public class Location
{
public int LocationID { get; set; }
public String Label { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public virtual ICollection<Recreation> Recreations { get; set; }
}
娱乐:
/*
* Represents a recreation activity type, such as Hiking or Camping, that
* can be applied to any location as a tag (each Location may have 0 or more
* Recreation tags).
*/
public class Recreation
{
public int RecreationID { get; set; }
public string Label { get; set; }
public virtual ICollection<Location> Locations { get; set; }
}
桥实体:
/*
* Bridge entity to handle associations of Location and Recreation
*/
public class LocationRecreation
{
[Key]
[ForeignKey("Location")]
[Column(Order = 1)]
[Display(Name = "Location")]
public int LocationID { get; set; }
[Key]
[ForeignKey("Recreation")]
[Column(Order = 2)]
[Display(Name = "Recreation")]
public int RecreationID { get; set; }
public virtual Location Location { get; set; }
public virtual Recreation Recreation { get; set; }
}
当我添加迁移和 运行 update-database
时,这适用于 controllers/views 的默认脚手架。但是,在查看“服务器资源管理器”选项卡时,我看到以下 tables:
Locations
Recreations
LocationRecreations
RecreationLocations
(额外的table?)
视图在 LocationRecreations
上处理 CRUD,但虚拟属性指向 RecreationLocations
,因此它们不起作用,因为 table 仍然是空的。
What is causing the migrations to create a duplicate table? Can I modify my models in some way to allow for only one table to be created, and so that the virtual properties function as intended?
编辑:
我在 Visual Studio 托管的新项目 here on Github 中重现了错误。为清楚起见,我选择了个人用户帐户,因此在 IdentityModels.cs
文件中为我的数据库上下文使用单个 ApplicationDbContext
:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public DbSet<Location> Locations { get; set; }
public DbSet<Recreation> Recreations { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
// Associate a Location with a Recreation.
public void AddOrUpdateRecreationLocation(string locationLabel, string recreationLabel)
{
var location = this.Locations.SingleOrDefault(l => l.Label == locationLabel);
var recreation = location.Recreations.SingleOrDefault(r => r.Label == recreationLabel);
//i if it does not exist, register the item.
if (recreation == null) location.Recreations.Add(this.Recreations.Single(r => r.Label == recreationLabel));
}
}
我解决了我的问题。 Location
和 Relation
都需要一个 virtual ICollection<LocationRelation>
(我的桥实体的集合)。问题是我告诉 entity framework 间接指向相关项目,因此它推断为我创建了第三个桥 table。
LocationRecreation
和 DbContext
代码没问题。以下是修改后的 Location
和 Recreation
模型:
/*
* Represents a location on a map.
*/
public class Location
{
public int LocationID { get; set; }
public String Label { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public virtual ICollection< LocationRecreation > LocRecs { get; set; }
}
/*
* Represents a recreation activity type, such as Hiking or Camping, that
* can be applied to any location as a tag (each Location may have 0 or more
* Recreation tags).
*/
public class Recreation
{
public int RecreationID { get; set; }
public string Label { get; set; }
public virtual ICollection< LocationRecreation > LocationRecs { get; set; }
}
我正在使用 MVC 5 和 EF 6 设计一个 ASP.NET 的 Web 应用程序。我们的团队使用代码优先迁移来设计我们的数据库。在我们的项目中,我们有两个模型 - Location
和 Recreation
。 Recreation 本质上是一个可枚举的标签,可以应用于任何 Location(多对多关系),所以我设计了一个 3rd bridge 实体模型来处理关系(所以我可以在代码中引用它)。以下是缩写的模型定义:
地点:
/*
* Represents a location on a map.
*/
public class Location
{
public int LocationID { get; set; }
public String Label { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public virtual ICollection<Recreation> Recreations { get; set; }
}
娱乐:
/*
* Represents a recreation activity type, such as Hiking or Camping, that
* can be applied to any location as a tag (each Location may have 0 or more
* Recreation tags).
*/
public class Recreation
{
public int RecreationID { get; set; }
public string Label { get; set; }
public virtual ICollection<Location> Locations { get; set; }
}
桥实体:
/*
* Bridge entity to handle associations of Location and Recreation
*/
public class LocationRecreation
{
[Key]
[ForeignKey("Location")]
[Column(Order = 1)]
[Display(Name = "Location")]
public int LocationID { get; set; }
[Key]
[ForeignKey("Recreation")]
[Column(Order = 2)]
[Display(Name = "Recreation")]
public int RecreationID { get; set; }
public virtual Location Location { get; set; }
public virtual Recreation Recreation { get; set; }
}
当我添加迁移和 运行 update-database
时,这适用于 controllers/views 的默认脚手架。但是,在查看“服务器资源管理器”选项卡时,我看到以下 tables:
Locations
Recreations
LocationRecreations
RecreationLocations
(额外的table?)
视图在 LocationRecreations
上处理 CRUD,但虚拟属性指向 RecreationLocations
,因此它们不起作用,因为 table 仍然是空的。
What is causing the migrations to create a duplicate table? Can I modify my models in some way to allow for only one table to be created, and so that the virtual properties function as intended?
编辑:
我在 Visual Studio 托管的新项目 here on Github 中重现了错误。为清楚起见,我选择了个人用户帐户,因此在 IdentityModels.cs
文件中为我的数据库上下文使用单个 ApplicationDbContext
:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public DbSet<Location> Locations { get; set; }
public DbSet<Recreation> Recreations { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
// Associate a Location with a Recreation.
public void AddOrUpdateRecreationLocation(string locationLabel, string recreationLabel)
{
var location = this.Locations.SingleOrDefault(l => l.Label == locationLabel);
var recreation = location.Recreations.SingleOrDefault(r => r.Label == recreationLabel);
//i if it does not exist, register the item.
if (recreation == null) location.Recreations.Add(this.Recreations.Single(r => r.Label == recreationLabel));
}
}
我解决了我的问题。 Location
和 Relation
都需要一个 virtual ICollection<LocationRelation>
(我的桥实体的集合)。问题是我告诉 entity framework 间接指向相关项目,因此它推断为我创建了第三个桥 table。
LocationRecreation
和 DbContext
代码没问题。以下是修改后的 Location
和 Recreation
模型:
/*
* Represents a location on a map.
*/
public class Location
{
public int LocationID { get; set; }
public String Label { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public virtual ICollection< LocationRecreation > LocRecs { get; set; }
}
/*
* Represents a recreation activity type, such as Hiking or Camping, that
* can be applied to any location as a tag (each Location may have 0 or more
* Recreation tags).
*/
public class Recreation
{
public int RecreationID { get; set; }
public string Label { get; set; }
public virtual ICollection< LocationRecreation > LocationRecs { get; set; }
}