注意:使用的技术是 ASP.Net MVC 3、Entity、SQL Server Management Studio
问题?
似乎当我运行时,上下文为:public class DatabaseInit : DropCreateDatabaseAlways<LocationAppContext>
它创建了数据库,但是我的服务分配表有一个额外的外键
ServiceAssignment_Service
,它不应该调用。
我的服务分配模型是这样的:
namespace LocationApp.Models
{
public class ServiceAssignment
{
public int id { get; set; }
public int locationID { get; set; }
public int ServiceID { get; set; }
public virtual Location Location { get; set; }
public virtual ServiceAssignment Service { get; set;}
}
}
服务模式是这样的:
namespace LocationApp.Models
{
public class Service
{
public Service()
{
this.ServiceAssignments = new HashSet<ServiceAssignment>();
}
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public bool active { get; set; }
public string icon { get; set; }
public virtual ICollection<ServiceAssignment> ServiceAssignments { get; set; }
}
}
话虽如此,关系很简单:
服务分配有许多位置 ID 和服务 ID。
为什么会生成这个额外的外键?当前密钥,应该有:
- PK : 表的主 PK
- FK 1 : Location_ServiceAssignment
- FK 2:Service_ServiceAssignment
那些是他们的,这第三个怎么莫名其妙....
第二部分是:如果 id 2 的位置的服务 id 为 2,3,6,7 我如何获得所有服务 id 的返回,以便我可以将对象传递给服务查询以获取有关服务的所有信息根据身份证?
更新:
上下文类:
namespace LocationApp.DAL
{
public class LocationAppContext : DbContext
{
public DbSet<Content> Contents { get; set; }
public DbSet<Location> Locations { get; set; }
public DbSet<ServiceAssignment> ServiceAssignments { get; set; }
public DbSet<Service> Services { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Location>().HasMany(sa => sa.ServiceAssignments);
modelBuilder.Entity<Service>().HasMany(sa => sa.ServiceAssignments);
}
}
}