[英]one to many relations between 3 tables with entity code first
Lets say I have 3 tables such as cities, tours and tourdetails 可以说我有3张桌子,例如城市,旅行团和游览细节
I want to store data about cities which are included in tour and duration of visit for each city. 我想存储有关城市的数据,这些数据包含在每个城市的旅行和访问时间中。 each tour might have several cities. 每个游览可能有多个城市。
here is my code 这是我的代码
public class TourDetails
{
[Required, Key]
public int ID { get; set; }
[Required]
public int Duration { get; set; }
[Required]
public virtual City City { get; set; }
[Required]
public virtual Tour TourID { get; set; }
}
public class Tour
{
[Required, Key, MaxLength(5), MinLength(5)]
public string ID { get; set; }
[Required, MaxLength(20)]
public string TourName { get; set; }
[Required]
public int TotalDuration { get; set; }
[Required]
public decimal Price { get; set; }
[Required]
public virtual ICollection<TourDetails> TourDetails { get; set; }
}
public class City
{
[Required, Key]
public int ID { get; set; }
[Required, MaxLength(20)]
public string CityName { get; set; }
[Required]
public virtual ICollection<TourDetails> TourDetails { get; set; }
}
My problem is that when I compile code Entity Framework doesn't shows me TourDetails table in diagram at all (table are being added to base) I'm pretty new to Entity Framewrok and just trying to learn so it might be a silly question but I hope I will get a proper answer. 我的问题是,当我编译代码时,Entity Framework根本没有在图中显示TourDetails表(表已添加到基础表中),我对Entity Framewrok还是很陌生,只是尝试学习所以它可能是一个愚蠢的问题,但是我希望我能得到一个正确的答案。
Issue is solved, problem was with my sql manager studio not with code, thanks a lot who tried to help and sorry for your time :) 问题已解决,问题出在我的sql manager studio而不是代码上,非常感谢那些尝试提供帮助的人,对不起您的时间:)
You are lacking ids of the entities (and one of properties' name is wrong): 您缺少实体的ID(并且属性名称之一是错误的):
public int CityId { get; set;}
public virtual City City { get; set; }
public int TourId { get; set; }
public virtual Tour Tour { get; set; }
Entity Framework should now detect the relationship 实体框架现在应该检测到关系
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.