简体   繁体   English

EF Code首先,如何从一个表到另一个表实现两个外键?

[英]EF Code First, how can I achieve two foreign keys from one table to other table?

I've recently downloaded Entity Framework Code First CTP5 , and have a trouble with this scenario. 我最近下载了Entity Framework Code First CTP5 ,并且遇到了这种情况的问题。 I have two tables as follows: 我有两张表如下:

Members table :
  ID  
  Name

Comments table :  
  ID  
  Comment  
  CommentedMemberID  
  CommentMemberID

And, the data should be like the following: 而且,数据应如下所示:

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; }
}

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). 但是当我在我的cshtml上运行这些模型时,它会给我错误说“无法创建CommentMember实例”或类似的东西(抱歉,我已经更改了我的模型以进行EF Code First评估,因此无法重现相同的错误) 。

I've also tried to use OnModelCreating on the TestContext , but can't find any good instructions and don't know what to do. 我也尝试在TestContext上使用OnModelCreating ,但找不到任何好的指令而且不知道该怎么做。 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. 我看到了EF Code First CTP3的博客文章,似乎该版本中有一个RelatedTo属性,但现在它已经消失了。

Could anyone know how to get it work properly? 谁能知道如何让它正常工作? Or is this a totally wrong way to go with this scenario? 或者这是完全错误的方式吗?

Thanks, 谢谢,
Yoo

This is a special case and you need to use fluent API to configure your associations. 这是一种特殊情况,您需要使用流畅的API来配置您的关联。 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);
}

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

相关问题 带有3个外键的EF代码优先链接表 - EF Code First link table with 3 foreign keys 我在EF Code First方法中创建了一个表并将一列作为其他两个表的外键 - I Created a table and Made a column as Foreign Key to two other tables in EF Code First Approach 具有两个外键的代码第一个表,如果另一个具有值,则应为null - Code first table with two foreign keys one should be null if the other has value Entity Framework Core - 代码优先 - 两个外键 - 一张表 - Entity Framework Core - Code First - Two Foreign Keys - One Table 实体框架代码优先 - 来自同一个表的两个外键 - Entity Framework Code First - two Foreign Keys from same table 实体框架代码优先-来自同一表的两个可空和一个非空外键 - Entity Framework Code First- Two nullable and one non-null foreign keys from same table 首先创建多个外键,这些外键引用EF代码中的同一表 - Create multiple foreign keys referes to the same table in EF code first MVC建模:表中有多个外键,代码优先EF - MVC modelbuilding: multiple foreign keys in table, code first EF 对于一个表EF 6具有2个外键的表无数据 - No data for table with 2 foreign keys to one table EF 6 如何在实体框架代码中首先从表的主键制作两个外键 - How to make two foreign keys from a primary key of a table in Entity Framework Code First
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM