Code First Joining Table,“表未声明属性的键”

时间:2014-04-23 11:10:05

标签: c# database entity-framework code-first

ASP .NET 4 MVC,Entity Framework 4.4的问题 上面相对缺乏经验,如果问题很明显就很抱歉,但是我难倒了,谷歌foo不起作用。

首先使用代码创建数据库(使用Devart MySQL dot net connector) 获得例外

* System.InvalidOperationException:外键组件“Clinician_ClientId”不是“Clinician_Client”类型的声明属性。  验证它是否未从模型中明确排除,并且它是有效的原始属性。

在我尝试使用有效负载实现连接表之前,事情进展顺利。 目的:客户可以有很多临床医生,临床医生可以有很多客户,有效负载是开始/结束时期。

下面的代码只是为了要点

  /// <summary>
/// Represents a clinician of the system
/// </summary>
public class Clinician
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int clinicianId { get; set; }


    [ForeignKey("UserDetails")]
    public int UserDetailsId { get; set; }
    public virtual UserDetails UserDetails { get; set; }

    [ForeignKey("Clinician_Client")]
    public int Clinician_ClientId { get; set; }
    public virtual ICollection<Clinician_Client> Clinician_Client { get; set; }
}

客户端类

/// <summary>
/// Represents a client of the system
/// </summary>
public class Client
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int clientId { get; set; }

    //Snip other fields

    [ForeignKey("UserDetails")]
    public int UserDetailsId { get; set; }
    public virtual UserDetails UserDetails { get; set; }

    [ForeignKey("Clinician_Client")]
    public int Clinician_ClientId { get; set; }
    public virtual ICollection<Clinician_Client> Clinician_Client { get; set; }


}

加入表

/// <summary>
/// Represents a clinicians care period over a client
/// </summary>
public class Clinician_Client
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Clinician_ClientId { get; set; }

    public DateTime start_dttm { get; set; }
    public DateTime end_dttm { get; set; }

    //foreign keys
//    [ForeignKey("Client")]
    public int ClientId { get; set; }
//    public virtual Client Client { get; set; }

//    [ForeignKey("Clinician")]
    public int ClinicianId { get; set; }
//    public virtual Clinician Clinician { get; set; }
}

上下文

  public class UsersContext : DbContext
{
    public UsersContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<UserDetails> UserDetails { get; set; }
    public DbSet<Client> Client { get; set; }
    public DbSet<Clinician> Clinician { get; set; }
    public DbSet<Clinician_Client> Clinician_Client { get; set; }
}

尝试过使用nullable(连接是可选的),连接表上的外键,无济于事。问题是我不理解错误,该字段是类和模型的一部分。

1 个答案:

答案 0 :(得分:0)

这是你的问题: -

[ForeignKey("Clinician_Client")]
public int Clinician_ClientId { get; set; }
public virtual ICollection<Clinician_Client> Clinician_Client { get; set; }

您认为Clinician_ClientId的价值应该是什么?

它不能是相关Clinician_ClientId对象的Clinician_Client,因为您希望每个Clinician(以及每个Client)与 <相关联em>很多 Clinician_Client对象。

看起来模型构建器正在查看类型ICollection<Clinician_Client>以查找与Clinician_ClientId匹配的属性(例如“Id”)。它没有,因为它是ICollection<Clinician_Client>,而不是Clinician_Client

另外,您可能需要考虑为Clinician_Client对象提供更好的名称来描述其有效负载。也许是“CarePeriod”?

尝试更多类似的内容: -

public class Clinician
{
  public int ClinicianId { get; set; }

  public virtual ICollection<CarePeriod> CarePeriods { get; set; }

  ...
}

public class Client
{
  public int ClientId { get; set; }

  public virtual ICollection<CarePeriod> CarePeriods { get; set }

  ...
}

public class CarePeriod
{
  public int CarePeriodId { get; set; }

  public int ClinicianId { get; set; }
  public virtual Clinician Clinician { get; set; }

  public int ClientId { get; set; }
  public virtual Client Client { get; set; }

  ...
}

不应该需要使用[Key]属性等等。默认约定应该包括你,除非我遗漏了你的连接器。