Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 3个表的代码优先设计(一对一)_Entity Framework_Database Design_Ef Code First - Fatal编程技术网

Entity framework 3个表的代码优先设计(一对一)

Entity framework 3个表的代码优先设计(一对一),entity-framework,database-design,ef-code-first,Entity Framework,Database Design,Ef Code First,我正在尝试创建一个数据库,用于跟踪正在邮寄的包裹。我一直在努力找出在任何给定时间/日期实现包裹与其位置之间关系的最佳方法。最终,我需要能够回顾过去的任何时刻,并告诉您包裹在哪里以及在什么时间/日期。当它进入一个位置时,当它离开一个位置时 我有三张桌子。简而言之 public class Parcel { public int ParcelID { get; set; } public virtual Location Location { get; set; } } publ

我正在尝试创建一个数据库,用于跟踪正在邮寄的包裹。我一直在努力找出在任何给定时间/日期实现包裹与其位置之间关系的最佳方法。最终,我需要能够回顾过去的任何时刻,并告诉您包裹在哪里以及在什么时间/日期。当它进入一个位置时,当它离开一个位置时

我有三张桌子。简而言之

public class Parcel
{
    public int ParcelID { get; set; }

    public virtual Location Location { get; set; }
}

public enum LocationType
{
    Warehouse, Truck
}
public class Location
{
    [Key, ForeignKey("Parcel")]
    public int ParcelID { get; set; }
    public LocationType LocationType { get; set; }

    public virtual Parcel Parcel { get; set; }
}
LocationTimeDate
{

}
我不知道如何将LocationTimeDate与位置关联起来。我认为应该是一对一的关系。所以我必须有LocationId。但地理位置的关键是一块外国钥匙


我这样做对吗?如果您有任何想法/指导,我们将不胜感激。

对于我来说,这是一个很好的案例,说明了多对多关系与附加信息位置、时间日期和包裹之间的关系。地块在不同时间可以有多个位置,并且一个位置可能在不同时间属于多个地块。我希望我能正确地理解你

以下是我建议的设计:

public class Parcel
{
    public int ParcelId { get; set; }

    public List<ParcelAssignment> ParcelAssignments { get; set; }
}

public class Location
{
    [Key]
    public int LocationId { get; set; }

    public LocationType LocationType { get; set; }

    public List<ParcelAssignment> ParcelAssignments { get; set; }
}

public enum LocationType
{
    Warehouse, Truck
} 

public class ParcelAssignment
{
    [Key]
    public int ParcelAssignmentId { get; set; }


    [Required]
    public int LocationId { get; set; }

    [ForeignKey("LocationId")]
    public Location Location { get; set; }


    [Required]
    public int ParcelId { get; set; }

    [ForeignKey("ParcelId")]
    public Parcel Parcel { get; set; }

    public DateTime DateOfAssignment { get; set; }
}
连接到dbcontext类的fluent api:

modelBuilder.Entity<ParcelAssignment>()
            .HasRequired(pa => pa.Parcel)
            .WithMany(p => p.ParcelAssignments)
            .HasForeignKey(pa => pa.ParcelId);

modelBuilder.Entity<ParcelAssignment>()
            .HasRequired(pa => pa.Location)
            .WithMany(l => l.ParcelAssignments)
            .HasForeignKey(pa => pa.LocationId);
您可以在此处阅读有关EF上多对多关系的更多信息:


是的,你知道我想要什么。我喜欢你的想法。今天晚上我做完工作后会测试一下,然后再给你回复。非常感谢您的反馈!谢谢你,埃尔卡纳。你的回答很有帮助。