Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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# EF ModelValidationException类型中的每个属性名称都必须是唯一的_C#_Entity Framework_Entity Framework 6_Ef Fluent Api - Fatal编程技术网

C# EF ModelValidationException类型中的每个属性名称都必须是唯一的

C# EF ModelValidationException类型中的每个属性名称都必须是唯一的,c#,entity-framework,entity-framework-6,ef-fluent-api,C#,Entity Framework,Entity Framework 6,Ef Fluent Api,在实体框架中,我试图基于此配置0到多个关系。在这里,学生可以有0个或多个联系人。但代码在运行时显示以下异常 System.Data.Entity.ModelConfiguration.ModelValidationException StudentId:Name:类型中的每个属性名称都必须是唯一的。所有物 已定义名称“StudentId” 我也试过移除HasForeignKey。但是什么也没用。你忘了为学生申报钥匙 modelBuilder.Entity<Student>().Has

在实体框架中,我试图基于此配置0到多个关系。在这里,学生可以有0个或多个联系人。但代码在运行时显示以下异常

System.Data.Entity.ModelConfiguration.ModelValidationException

StudentId:Name:类型中的每个属性名称都必须是唯一的。所有物 已定义名称“StudentId”


我也试过移除HasForeignKey。但是什么也没用。

你忘了为学生申报钥匙

modelBuilder.Entity<Student>().HasKey(x => x.StudentId);

首先,我认为合适的关系是一对多,一个学生可以有从0到多的多个联系人

我是这样建立关系的:

public class Student
{
    public Student()
    {
        Contacts = new HashSet<Contacts>();
    }

    public int StudentId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Contact> Contacts { get; set; }
}

public class Contact 
{
    public int ContactId { get; set; }
    public string Name { get; set; }
    public int StudentId { get; set; } //usually I use Annotations here to create the DB like.. [Column("column_name")]
    //Here I use [InverseProperty(Contact), ForeignKey("StudentId")]
    public virtual Student Student { get; set; }
}

public static void Configure(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Contacts>.HasOne(a => a.Student).WithMany(b => b.Contacts).HasForeignKey(a => a.StudentId);    
}

它应该在没有注释的情况下工作。

糟糕的是,教程根本没有反映示例代码:-remove.WithRequirede=>e.Student.HasForeignKeye=>e.StudentId;从Student模型生成器-Student本身通常没有外键-EF尝试将该外键添加为属性,但您已经将其作为主键。我尝试了此方法,也尝试了在实体中添加[key]属性。尝试从Contact中删除StudentId时无效,ef不需要它,并将在数据库中自动生成它,您可以测试导致问题的StudentId属性
public class Student
{
    public Student()
    {
        Contacts = new HashSet<Contacts>();
    }

    public int StudentId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Contact> Contacts { get; set; }
}

public class Contact 
{
    public int ContactId { get; set; }
    public string Name { get; set; }
    public int StudentId { get; set; } //usually I use Annotations here to create the DB like.. [Column("column_name")]
    //Here I use [InverseProperty(Contact), ForeignKey("StudentId")]
    public virtual Student Student { get; set; }
}

public static void Configure(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Contacts>.HasOne(a => a.Student).WithMany(b => b.Contacts).HasForeignKey(a => a.StudentId);    
}