我正在创建一个应用以下逻辑的模式:AString可以属于多个位置。MultipleLocations可以有多个String,也可以没有 String。必须记录形成和之间关系的DateTime(As DateScraped) 。LocationString基本上,我使用连接表将关系映射为多对多关系,如下所示:在 Code First EF6 (Using SQLite) 中映射图表时,我有以下对象:public class Location{ [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long LocationId { get; set; } [Required] public string Country { get; set; } [Required] public string CityOrProvince { get; set; } [Required] public string PlaceOrCity { get; set; } [Required] public string PostalCode { get; set; }}public class String{ [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long StringId { get; set; } [Required] public string SearchString { get; set; }}public class LocationStringMapping{ [Required] public string LocationId { get; set; } [Required] public string StringId { get; set; } [Required] public DateTime DateScraped { get; set; }}到目前为止,我所做的一切都是基于猜想,因为我似乎找不到任何关于必须如何建立这种关系的具体信息。通常我会使用联结表,但那是在普通 SQL 中。EF 中的实现是否不同?我将不得不LocationStringMapping手动繁琐地管理表格,还是有某种我不知道的隐式关系模型?
1 回答
撒科打诨
TA贡献1934条经验 获得超2个赞
public class Location
{
public long LocationId {get;set;}
public virtual ICollection<LocationStringMapping> LocationStringMappings {get;set;}
//other
}
public class String
{
public long StringId {get;set;}
public virtual ICollection<LocationStringMapping> LocationStringMappings {get;set;}
//other
}
public class LocationStringMapping
{
[Key, Column(Order = 0)]
public long LocationId { get; set; }
[Key, Column(Order = 1)]
public long StringId { get; set; }
public virtual Location Location {get;set;}
public virtual String String {get;set;}
public DateTime DateScraped {get;set;}
}
- 1 回答
- 0 关注
- 170 浏览
添加回答
举报
0/150
提交
取消