Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.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# 首先使用fluent API在EF代码中定义一对一_C#_Entity Framework - Fatal编程技术网

C# 首先使用fluent API在EF代码中定义一对一

C# 首先使用fluent API在EF代码中定义一对一,c#,entity-framework,C#,Entity Framework,我有两个代码fist POCO(任命人和案例): 在我们的术语中,被任命者是Profile的同义词。因此,我们任命的[关键]是ProfileID 被任命者不必指定案例,因此我将CaseID设置为null int-int 从这里我得到了一个错误,比如,不能在案例和被任命者之间确定端点 我认为问题在于以防万一。 ProfileID是被任命者的外键,应该是虚拟被任命者属性的导航属性。 但我不认为它理解导航道具不是指定的而是ProfileID 所以我把它放在DbContext中: protected o

我有两个代码fist POCO(任命人和案例):

在我们的术语中,被任命者是Profile的同义词。因此,我们任命的[关键]是ProfileID

被任命者不必指定案例,因此我将CaseID设置为null int-int

从这里我得到了一个错误,比如,不能在案例和被任命者之间确定端点

我认为问题在于以防万一。 ProfileID是被任命者的外键,应该是虚拟被任命者属性的导航属性。 但我不认为它理解导航道具不是指定的而是ProfileID

所以我把它放在DbContext中:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Appointee>().HasKey(a => a.ProfileID);
        modelBuilder.Entity<Case>().HasKey(c => c.CaseID);
        modelBuilder.Entity<Case>().HasRequired(c => c.Appointee);
        modelBuilder.Entity<Appointee>().HasOptional(a => a.Case);
    }
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity().HasKey(a=>a.ProfileID);
modelBuilder.Entity().HasKey(c=>c.CaseID);
modelBuilder.Entity().HasRequired(c=>c.appointe);
modelBuilder.Entity().has可选(a=>a.Case);
}
现在我得到:无效的列名'Appointe_ProfileID'

如何正确设置此问题。

最终解决了此问题。 这里有几件事。 我习惯于用AutoInc设置代理主键,所以我为被任命者和案例设置了AutoInc ID

实际上,被任命者的关键应该是ProfileID

接下来,在实体框架中,依赖表中的外键必须与父表中的主键相同,EF称之为原则

因此,一旦我意识到我的密钥都是PK和FK,就必须是ProfileID,我做了以下修复

public class Appointee
{
    [Key]
    public string ProfileID { get; set; }
    public string FistName { get; set; }
    public string LastName { get; set; }
    public DateTime Dob { get; set; }
    public string Ssn { get; set; }
    public string GrantNumber { get; set; }

    public virtual Case Case { get; set; }
}

public class Case
{
    [Key, ForeignKey("Appointee")]
    public string ProfileID { get; set; }
    public int PSCStatusID { get; set; }

    [ForeignKey("ProfileID")]
    public virtual Appointee Appointee { get; set; }
}
注意案例中的[Key,ForeignKey(“Appointe”)]属性,我需要告诉EF Appointe是原则。

我打赌这将有助于:
public class Appointee
{
    [Key]
    public string ProfileID { get; set; }
    public string FistName { get; set; }
    public string LastName { get; set; }
    public DateTime Dob { get; set; }
    public string Ssn { get; set; }
    public string GrantNumber { get; set; }

    public virtual Case Case { get; set; }
}

public class Case
{
    [Key, ForeignKey("Appointee")]
    public string ProfileID { get; set; }
    public int PSCStatusID { get; set; }

    [ForeignKey("ProfileID")]
    public virtual Appointee Appointee { get; set; }
}