EF代码首先,如何从一个表到其他表中实现两个外键? [英] EF Code First, how can I achieve two foreign keys from one table to other table?

查看:96
本文介绍了EF代码首先,如何从一个表到其他表中实现两个外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近下载了实体框架代码第一个CTP5 ,并且在这种情况下有问题。我有两张表如下:

I've recently downloaded Entity Framework Code First CTP5, and have a trouble with this scenario. I have two tables as follows:


Members table :
  ID  
  Name

Comments table :  
  ID  
  Comment  
  CommentedMemberID  
  CommentMemberID  

而且,数据应该如下:


Members  
ID Name   
1  Mike  
2  John  
3  Tom  

Comments  
ID Comment CommentedMemberID CommentMemberID  
1  Good 1 2       
2  Good 1 3  
3  Bad  2 1  

然后,我编码如下所示:

Then, I coded as shown below:

public class Member
{
    public int ID {get; set; }
    public string Name { get; set;}
    public virtual ICollection<Comment> Comments { get; set;}
}

public class Comment
{
    public int ID { get; set; }
    public string Comment { get; set; }
    public int CommentedMemberID { get; set; }
    public int CommentMemberID{ get; set; }

    public virtual Member CommentedMember { get; set; }
    public virtual Member CommentMember { get; set; }
}

public class TestContext : DbContext
{
    public DbSet<Member> Members { get; set; }
    public DbSet<Comment> Comments { get; set; }
}

但是当我在我的 cshtml上运行这些模型,它给我错误说不能创建 CommentMember 实例或类似的东西(对不起,我已经改变了我的模型继续EF代码第一评估,所以不能再现同样的错误)。

But when I run these models on my cshtml, it gives me errors saying "Cannot create CommentMember instance" or something like that (Sorry, I already changed my models to proceed the EF Code First evaluation, so can't reproduce the same error).

我也尝试使用 OnModelCreating TestContext ,但找不到任何好的说明,不知道该怎么办。我看到了EF Code First CTP3的博客文章,似乎在该版本中有一个RelatedTo属性,但现在已经走了。

I've also tried to use OnModelCreating on the TestContext, but can't find any good instructions and don't know what to do. I saw a blog post of the EF Code First CTP3, and it seems there was a RelatedTo attribute in that version, but now it has gone.

有谁知道如何让它正常工作?或者这是一个完全错误的方式去与这种情况吗?

Could anyone know how to get it work properly? Or is this a totally wrong way to go with this scenario?

谢谢,

Yoo

Thanks,
Yoo

推荐答案

这是一个特殊情况,您需要使用流畅的API来配置您的关联。这将是诀窍:

This is a special case and you need to use fluent API to configure your associations. This will do the trick:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Comment>().HasRequired(c => c.CommentedMember)
                                  .WithMany(m => m.Comments)
                                  .HasForeignKey(c => c.CommentedMemberID)
                                  .WillCascadeOnDelete();

    modelBuilder.Entity<Comment>().HasRequired(c => c.CommentMember)
                                  .WithMany()
                                  .HasForeignKey(c => c.CommentMemberID)
                                  .WillCascadeOnDelete(false);
}

这篇关于EF代码首先,如何从一个表到其他表中实现两个外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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