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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.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# 基于fluentapi的实体框架中的一对多映射_C#_Entity Framework_Ef Code First_Mapping_Relationship - Fatal编程技术网

C# 基于fluentapi的实体框架中的一对多映射

C# 基于fluentapi的实体框架中的一对多映射,c#,entity-framework,ef-code-first,mapping,relationship,C#,Entity Framework,Ef Code First,Mapping,Relationship,我找到了很多关于这方面的例子,但没有人适合我 我的班级是: public class Class1 { public int id { get; set; } public string desc { get; set; } public virtual Class2 class2 {get; set;} } 第二类: public class Class2 { public int id { get; set; } public string des

我找到了很多关于这方面的例子,但没有人适合我

我的班级是:

public class Class1
{
    public int id { get; set; }
    public string desc { get; set; } 
    public virtual Class2 class2 {get; set;}
}
第二类:

public class Class2
{
    public int id { get; set; }
    public string desc { get; set; } 
}
我正在以这种方式映射
Class1

public class Class1Map : EntityTypeConfiguration<Class1>
{
    public Class1Map()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Table & Column Mappings
        this.ToTable("Class1TableName");
        this.Property(t => t.id).HasColumnName("IdColumnName");
        this.Property(t => t.desc).HasColumnName("DescColumName");
    }
}

解决了,是代码迁移中的一个问题,我忘记了更新代码迁移类,所以EF使用了旧的列名

正确的映射语法是

this.HasRequired(t => t.class2)
    .WithRequiredDependent()
    .Map(m => m.MapKey("IdClass2ColumnName "));

在完成一些工作后,创建了数据库(即使在配置中设置的AutomaticMiggerationsEnabled属性不起作用…),但在ClassTableName中未找到IdClass2ColumnName字段,而是class2\u id ONE附加信息:表中的每个class2条目都可以用于许多class1条目,它是一个简单类型表
this.HasRequired(t => t.class2)
    .WithRequiredDependent()
    .Map(m => m.MapKey("IdClass2ColumnName "));