Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在实体框架模型中为ICollection定义外键_C#_Model_Reference_Entity Framework 6_Ef Database First - Fatal编程技术网

C# 在实体框架模型中为ICollection定义外键

C# 在实体框架模型中为ICollection定义外键,c#,model,reference,entity-framework-6,ef-database-first,C#,Model,Reference,Entity Framework 6,Ef Database First,如何在实体框架6中定义哪些列将用于引用ICollection中的数据 例如,我们有一个简单的用户信息关系,如果我可以从更新或创建信息的用户处获取所有信息: public class User { public int UserId { get; get; } // Here is the problem, how to define that EF // Has to use the CreateUser column

如何在
实体框架6
中定义哪些列将用于引用
ICollection
中的数据

例如,我们有一个简单的用户信息关系,如果我可以从更新或创建信息的用户处获取所有信息:

public class User
{
    public int UserId
    {
        get;
        get;
    }

    // Here is the problem, how to define that EF
    // Has to use the CreateUser column
    public virtual ICollection<User> User_CreateUser
    {
        get;
        set;
    }   

    // Here is the problem, how to define that EF
    // Has to use the UpdateUser column
    public virtual ICollection<User> User_UpdateUser
    {
        get;
        set;
    }   
}

public class Information
{
    public int InfoId
    {
        get;
        set;            
    }   

    public int CreateUser
    {
        get;
        set;            
    }

    public int UpdateUser
    {
        get;
        set;            
    }

    [System.ComponentModel.DataAnnotations.Schema.ForeignKey("CreateUser")]
    public virtual User User_CreateUser
    {
        get;
        set;
    }

    [System.ComponentModel.DataAnnotations.Schema.ForeignKey("UpdateUser")]
    public virtual User User_UpdateUser
    {
        get;
        set;
    }       
}   
公共类用户
{
公共int用户标识
{
得到;
得到;
}
//问题是,如何定义EF
//必须使用CreateUser列
公共虚拟ICollection用户\u CreateUser
{
得到;
设置
}   
//问题是,如何定义EF
//必须使用UpdateUser列
公共虚拟ICollection用户\u UpdateUser
{
得到;
设置
}   
}
公开班级信息
{
公共int InfoId
{
得到;
设置
}   
公共int CreateUser
{
得到;
设置
}
公共整数更新程序
{
得到;
设置
}
[System.ComponentModel.DataAnnotations.Schema.ForeignKey(“CreateUser”)]
公共虚拟用户\u CreateUser
{
得到;
设置
}
[System.ComponentModel.DataAnnotations.Schema.ForeignKey(“UpdateUser”)]
公共虚拟用户\u UpdateUser
{
得到;
设置
}       
}   
但我不知道,如何绘制这张地图。对于简单的外键,它很容易超越注释

我首先使用数据库,但自己生成模型。(除了
ICollection
之外,它还可以很好地工作


谢谢!

您已将第二个外键标记为
User
,但它应该是
int

public int UpdateUser { get; set; }
试试这个:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

namespace Test
{
    public class User
    {
        public int UserId { get; set; }

        public virtual ICollection<User> User_CreateUser { get; set; }

        public virtual ICollection<User> User_UpdateUser { get; set; }
    }

    public class Information
    {
        public int InfoId { get; set; }

        public int CreateUser { get; set; }

        public int UpdateUser { get; set; }

        [ForeignKey("CreateUser")]
        public virtual User User_CreateUser { get; set; }

        [ForeignKey("UpdateUser")]
        public virtual User User_UpdateUser { get; set; }
    }
}
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations.Schema;
名称空间测试
{
公共类用户
{
public int UserId{get;set;}
公共虚拟ICollection用户\u CreateUser{get;set;}
公共虚拟ICollection用户_UpdateUser{get;set;}
}
公开班级信息
{
公共int InfoId{get;set;}
public int CreateUser{get;set;}
public int UpdateUser{get;set;}
[外键(“CreateUser”)]
公共虚拟用户用户\u CreateUser{get;set;}
[外键(“更新用户”)]
公共虚拟用户用户\u UpdateUser{get;set;}
}
}

谢谢,这是我的错,因为我为SO问题编写了代码。但这并没有解决我的问题:D@BendEg但它应该:)问题就在别的地方。它会抛出一些异常吗?好吧,也许这个问题的描述不好。我的意思是,实体框架如何将
User\u CreateUser
映射到
CreateUser
User\u UpdateUser
映射到
UpdateUser
?超过名称?@BendEg如果POCO只包含一个这样的类型,则EF遵循“约定超过配置”并执行此操作。在您的情况下(不止一种类似类型),您必须手动放置
ForeignKey
属性(就像您所做的那样)或覆盖
OnModelCreating
方法和映射列。好吧,那么ForeignKey也可以处理一对多?这是重要的信息,谢谢!