C# 在实体框架中表示连接表

C# 在实体框架中表示连接表,c#,entity-framework,ef-code-first,many-to-many,junction-table,C#,Entity Framework,Ef Code First,Many To Many,Junction Table,我正在创建一个模式,其中应用了以下逻辑: 字符串可以属于多个位置 多个位置可以有多个字符串,或者没有 字符串 必须记录形成位置和字符串之间关系的日期时间(作为DateScraped) 基本上,我使用如下连接表将关系映射为多对多关系: 在代码优先EF6(使用SQLite)中映射图表时,我有以下对象: public class Location { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public

我正在创建一个模式,其中应用了以下逻辑:

  • 字符串可以属于多个位置
  • 多个
    位置
    可以有多个
    字符串
    ,或者没有
    字符串
  • 必须记录形成
    位置
    字符串
    之间关系的
    日期时间
    (作为
    DateScraped
基本上,我使用如下连接表将关系映射为多对多关系:

在代码优先EF6(使用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
表,或者是否存在某种我不知道的隐式关系模型

公共类位置
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;}
}
{ 公共长位置ID{get;set;} 公共虚拟ICollection LocationStringMappings{get;set;} //其他 } 公共类字符串 { 公共长字符串ID{get;set;} 公共虚拟ICollection LocationStringMappings{get;set;} //其他 } 公共类LocationStringMapping { [键,列(顺序=0)] 公共长位置ID{get;set;} [键,列(顺序=1)] 公共长字符串ID{get;set;} 公共虚拟位置{get;set;} 公共虚拟字符串{get;set;} public DateTime DateScraped{get;set;} }
我想可能会有帮助。它们的关键术语是导航属性。还有,可能会有帮助。谢谢!我没想到像这样的隐性关系会如此简单,但在测试您的解决方案后,它按预期工作!