简体   繁体   English

如何与实体框架建立一对多关系?

[英]How do I represent a one to many relationship with the Entity Framework?

How do I represent a one to many relationship in ASP.NET MVC with the Entity Framework? 如何用实体框架表示ASP.NET MVC中的一对多关系?

My project is a conference calling project. 我的项目是电话会议项目。 I have a database of Conferences . 我有一个Conferences数据库。 Each virtual Conference room has a Title , an OwnerName , and a list of different DialInNumbers . 每个虚拟Conference室都有一个Title ,一个OwnerName和一个不同的DialInNumbers列表。 I know that in order to make this relationship of one Conference to many DialInNumbers , I'll need at least two tables: one for the Conferences , and one for the DialInNumbers at least. 我知道,为了使一个Conference与许多DialInNumbers这种关系,我至少需要两个表:一个用于Conferences ,一个至少用于DialInNumbers I've been having problems creating, modifying and accessing the DialInNumbers . 我在创建,修改和访问DialInNumbers遇到问题。 My code is definitely missing something. 我的代码肯定缺少某些内容。 Let's take a look: 让我们来看看:

Here is the model for the Conference : 这是Conference的模型:

public class Conference
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string OwnerName { get; set; }
    public List<string> DialInNumbers { get; set; }
}

The model requires a DbContext (not really sure why...) 该模型需要一个DbContext (不确定原因)。

public class ConferenceDbContext : DbContext
{
    public DbSet<Conference> Conferences { get; set; }
}

Then, in the Controller ( ConferenceController ), we can Create and Edit a Conference . 然后,在ControllerConferenceController )中,我们可以CreateEdit Conference For example: 例如:

public ActionResult Create(Conference conference)
{
    if (ModelState.IsValid)
    {
        conference.DialInNumbers = ThreeRandomlyGeneratedPhoneNumbers(); 

        db.Conferences.Add(conference);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(conference);
}

When I save the database changes, I only get one table for the Conferences . 保存数据库更改时,我仅获得一张Conferences表。 How do I get multiple tables and how do I link them together in the Entity Framework? 如何获得多个表,以及如何在实体框架中将它们链接在一起?

Thank you. 谢谢。

You can't define 1:N relationship between the entity and a string. 您无法在实体和字符串之间定义1:N关系。 In the EF it's only possible to have a relationship between two entities, so DialInNumber should be represented by an entity. 在EF中,只能在两个实体之间建立关系,因此DialInNumber应该由一个实体表示。

public class DialInNumber {
   public int ID { get; set; }
   public string Number { get; set; }
   public virtual Conference Conference { get; set;}
}

And Conference class should reference a collection of these entities instead of the collection of strings. 并且Conference类应该引用这些实体的集合,而不是字符串的集合。

public class Conference {
    public string Title { get; set; }
    public string OwnerName { get; set; }
    public virtual ICollection<DialInNumber> DialInNumbers { get; set; }    
}

And a note: in order to make lazy-loading work, the DialInNumbers property should be virtual 和记:为了使延迟加载工作中, DialInNumbers属性应该是虚拟的

public class Conference
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string OwnerName { get; set; }
    public List<string> DialInNumbers { get; set; }
}

Your entity has only scalar properties. 您的实体只有标量属性。 This mean that it doesn't refer to another entity. 这意味着它没有引用另一个实体。 In your specific case, the property DialInNumbers is a list of string which is not a list of entity. 在您的特定情况下,属性DialInNumbers是一个字符串列表,而不是实体列表。 Entity Framework do navigation property into table relationship which is not the case now. 实体框架将导航属性转换为表关系,现在情况并非如此。

If you do want to have a table of DialInNumbers, you will need to have a list of your DialNumber. 如果确实要有一个DialInNumbers表,则需要一个DialNumber列表。

public class Conference
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string OwnerName { get; set; }
    public List<DialNumber> DialInNumbers { get; set; }
}

public Class DialNumber
{
    public string Number{get;set;}
    public int ConferenceId{get;set;}
    public Conference Conference{get;set;}
}

You need to configure your entity for the navigation property in your configuration class. 您需要在配置类中为导航属性配置实体。

modelBuilder
   .Entity<DialNumber>()
   .HasRequired(p => p.Conference);

If you want to use entity with the id you will need to modify you DialNumber to : 如果要使用具有ID的实体,则需要将DialNumber修改为:

public Class DialNumber
{
    public string Number{get;set;}
    public Conference Conference{get;set;}
}

modelBuilder
   .Entity<DialNumber>()
   .HasRequired(p => p.Conference);
   .WithMany(b => b.DialNumber)
   .HasForeignKey(p => p.ConferenceId);

This will allow you to have less overhead of attaching entity later on and will allow you to have your foreign key named the way you want, in this case ConferenceId instead of the default syntax which would have produce (like in the first case) "Conference_Id". 这将使您以后减少附加实体的开销,并使您的外键以所需的方式命名,在这种情况下,ConferenceId而不是将产生的默认语法(如第一种情况)“ Conference_Id ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何配置Entity Framework级联删除以正确处理一对多关系? - How do I configure Entity Framework cascade delete to work properly with a one to many relationship? 如何使用 Entity Framework Core 建立一对多关系? - How do I set up a one-to-many relationship using Entity Framework Core? 如何在Entity Framework 5中表达“有很多通过”的关系? - How do I express a “has many through” relationship in Entity Framework 5? 如何在实体框架中将一个实体映射到许多实体? - How do I map one entity to many entities in Entity Framework? 实体框架7一对多的关系 - Entity Framework 7 one to many relationship 实体框架6一对多关系 - Entity Framework 6 one to many relationship 实体框架一对多的关系 - Entity Framework One to many relationship 实体框架6中的一对多关系 - One to many relationship in Entity Framework 6 实体框架如何在多对多关系中插入唯一值而无需手动检查 - Entity Framework how do I INSERT unique value on many to many relationship without having to do manual check 使用Entity Framework 4.1,如何使用不同的字段名称配置与复合键的一对多关系? - Using Entity Framework 4.1, how do I configure one to many relationship with composite keys using different field names?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM