ASP.NET迁移正在创建重复的桥实体

时间:2015-02-20 02:40:40

标签: c# visual-studio-2013 asp.net-mvc-5 entity-framework-6 ef-migrations

我正在使用MVC 5和EF 6使用ASP.NET设计Web应用程序。我们的团队使用代码优先迁移来设计我们的数据库。在我们的项目中,我们有两个模型 - LocationRecreation。 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<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时,这适用于控制器/视图的默认支架。但是,在查看“服务器资源管理器”选项卡时,我会看到以下表格:

  • Locations
  • Recreations
  • LocationRecreations
  • RecreationLocations (额外的表?)

视图处理LocationRecreations上的CRUD,但虚拟属性指向RecreationLocations,因此它们无法正常工作,因为该表仍为空。

  

导致迁移创建重复表的原因是什么?我是否可以通过某种方式修改模型以仅允许创建一个表,以便虚拟属性按预期运行?

修改

我在托管here on Github的新Visual Studio项目中重新创建了错误。为清楚起见,我选择了单个用户帐户,因此使用ApplicationDbContext文件中的单IdentityModels.cs作为我的数据库上下文:

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));
    }
}

1 个答案:

答案 0 :(得分:1)

我解决了我的问题。 LocationRelation都需要virtual ICollection<LocationRelation>(我的桥实体的集合)。问题是我告诉实体框架间接指向相关项目,因此推断为我创建第三个桥接表。

LocationRecreationDbContext代码没问题。以下是修改后的LocationRecreation模型:

/*
 * 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; }
}