[英]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
. 然后,在
Controller
( ConferenceController
)中,我们可以Create
和Edit
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.