简体   繁体   English

首先使用实体​​代码的3个表之间的一对多关系

[英]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.

相关问题 通过EF4.3中的代码优先方法模型在实体之间创建多个(一对多)关系 - Create more than one (one to many) relations between to entity by code first approach model in EF4.3 实体框架代码表之间的前两个关系 - Entity Framework Code First two relations between tables 首先如何在代码中定义两个实体之间的多对多和一对多关系? - how to define many-to-many and one-to-many relations between two entities in code first? 首先在实体框架代码中连接具有一对多关系的两个表 - Joining two tables with one to many relatipnship in entity framework code first 实体框架代码优先:如何在两个表之间创建一对多和一对一的关系? - Entity Framework Code First: How can I create a One-to-Many AND a One-to-One relationship between two tables? 服务器端的实体框架代码优先 Blazor:相同表之间的两个关系问题 - Entity Framework Code First in Serverside Blazor: Problem with two relations between the same tables EF Core Code First,相同object的2个多对一关系 - EF Core Code First, 2 Many to one relations of same object 实体框架代码一个实体的“多对多” - Entity Framework Code First many to many for one entity 代码优先:同一实体的一对多关系 - Code first: One to many relationship of the same entity 实体框架代码优先从两个表和一对一的关系创建类 - Entity Framework Code First creates classes from two tables and relationships one to many
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM