1
我正在使用代码优先方法来基于模态和数据库上下文类创建数据库。问题是,当我创建一个模型和下一个模型之间的关系并运行代码时,数据库会像它应该那样生成外键。使用外键在实体中创建关系
如何我想要的关系,与出外键。这可能与实体框架和代码优先方法有关吗?
例如:
namespace LocApp.Models
{
public class Location
{
public int id { get; set; }
[Required]
public string name { get; set; }
[Required]
public string address { get; set; }
[Required]
public string city { get; set; }
[Required]
public string country { get; set; }
[Required]
public string province { get; set; }
[Required]
public string phone { get; set; }
public string fax { get; set; }
public bool active { get; set; }
public virtual ICollection<LocationAssignment> LocationAssignment { get; set; }
}
}
有关系:
namespace LocApp.Models
{
public class LocationAssignment
{
public int id { get; set; }
public int locationID { get; set; }
public int serviceID { get; set; }
public int productID { get; set; }
public int personID { get; set; }
public virtual Location Location { get; set; }
public virtual Service Service { get; set; }
public virtual Product product { get; set; }
public virtual Person person { get; set; }
}
}
正如你可以看到这个表将与位置表生成的外键。如何保持这种关系WITH OUT外键的生成?
为什么你不想要外键? – 2013-04-20 20:11:42
,因为显然公司并不知道如何管理数据库,只会在产品交付后放弃所有外键约束,这会破坏或破坏应用程序。所以我正在尝试使用不生成外键的代码优先方法创建表。然而,保持关系 – TheWebs 2013-04-20 20:14:23
这是最奇怪的使用代码首先我听到:)但有趣的 - 所以你实际上不希望'FK-s'在数据库生成的权利? (不仅仅是在您的POCO中显示为一个属性)。 – NSGaga 2013-04-20 21:13:11