在实体框架中表示连接表

我正在创建一个应用以下逻辑的模式:

  • AString可以属于多个位置。

  • MultipleLocations可以有多个String,也可以没有 String

  • 必须记录形成和之间关系的DateTime(As DateScraped) 。LocationString

基本上,我使用连接表将关系映射为多对多关系,如下所示:

http://img1.mukewang.com/60e00d690001faaf06870298.jpg

在 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手动繁琐地管理表格,还是有某种我不知道的隐式关系模型?


慕田峪7331174
浏览 152回答 1
1回答

撒科打诨

public class Location{&nbsp; &nbsp; public long LocationId {get;set;}&nbsp; &nbsp; public virtual ICollection<LocationStringMapping> LocationStringMappings {get;set;}&nbsp; &nbsp; //other}public class String{&nbsp; &nbsp; public long StringId {get;set;}&nbsp; &nbsp; public virtual ICollection<LocationStringMapping> LocationStringMappings {get;set;}&nbsp; &nbsp; //other}public class LocationStringMapping{&nbsp; &nbsp; [Key, Column(Order = 0)]&nbsp; &nbsp; public long LocationId { get; set; }&nbsp; &nbsp; [Key, Column(Order = 1)]&nbsp; &nbsp; public long StringId { get; set; }&nbsp; &nbsp; public virtual Location Location {get;set;}&nbsp; &nbsp; public virtual String String {get;set;}&nbsp; &nbsp; public DateTime DateScraped {get;set;}}
打开App,查看更多内容
随时随地看视频慕课网APP