实体框架代码第一 - 具有列表<>的另一个类到多个表? [英] Entity framework code first - map class with List<> of another class to multiple tables?

查看:115
本文介绍了实体框架代码第一 - 具有列表<>的另一个类到多个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在评估EF4,并有一个很基本的问题...我想,我似乎找不到答案..



以下示例:

  public class Question 
{
public string question {get; set;}
public string answer {get; set;}
}

public class Person
{
public string Id {get; set;}
public string Name {get; set;}
public List< Question>问题{get; set;}
}

然后我在数据库中有以下表格

  Person 

id,
name


问题

id,
personId,
问题,
答案,

我可以首先使用EF4代码将Person类映射到两个表,或者我可以重组我的POCO的第一个,所以问题类包含id和personId - 这不是我想要做的事情。



我可以在OnModelCreating中添加一些东西来映射类,因为我需要它来映射?



谢谢!

解决方案

好的,这是我现在所做的 - 但它需要我必须重组我的问题类...

  public class Question 
{
public int Id {get ; set;} / * New * /
public int PersonId {get; set;} / * New * /
public string question {get; set;}
publ ic string answer {get; set;}

public virtual Person PersonObj {get; set;}
}

public class Person
{
public string Id {得到; set;}
public string Name {get; set;}
public List< Question>问题{get;设置;}
}

,并在OnModelCreating事件中添加以下内容$ / b
$ b

  modelBuilder.Entity< Person>()。 
HasMany(d => d.Questions)。
WithRequired(c => c.Person)。
HasForeignKey(c => c.PersonId)。
WillCascadeOnDelete();

不知道这是完全正确的...但似乎现在正在工作。


I'm evaluating EF4 and have a pretty basic question...I think, that I can't seem to find an answer for..

take the following example:

public class Question
{
  public string question {get;set;}
  public string answer {get; set;}
}

public class Person
{
  public string Id {get; set;}
  public string Name {get; set;}
  public List<Question> Questions {get; set;}
}

Then I have the following tables in the database

Person
(
 id,
 name
)

Question
(
 id,
 personId,
 question,
 answer,
)

Can I use the EF4 code first to map the Person class to the two tables, or do I ahve to restructure my POCO's first so the question class contains the id and personId - which is not something I would like to do.

Can I add something to the OnModelCreating to map the class as I need it to be mapped?

Thanks!

解决方案

Ok here's what I've done for now - but it requires me having to restructure my question class...

public class Question
{  
    public int Id {get;set;}   /* New */
    public int PersonId {get;set;}  /* New */
    public string question {get;set;}  
    public string answer {get; set;}

    public virtual Person PersonObj {get;set;}
}

public class Person
{  
    public string Id {get; set;}  
    public string Name {get; set;}  
    public List<Question> Questions {get; set;}
}

and added the following in the OnModelCreating event

 modelBuilder.Entity<Person>().
                    HasMany(d => d.Questions).
                    WithRequired(c => c.Person).
                    HasForeignKey(c => c.PersonId).
                    WillCascadeOnDelete();

Not sure it's fully right...but seems to be working for now.

这篇关于实体框架代码第一 - 具有列表&lt;&gt;的另一个类到多个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆