如何在EF 6中的Fluent API中映射1到0或1关系

时间:2019-06-03 13:36:30

标签: c# asp.net-mvc entity-framework-6

已编辑

我有2张桌子,护士和人。人与护士有1比0或1关系。换句话说,我申请中的某些人也将是护士。我想在两个表中使用相同的主键。当我运行代码查询Nurse表时,最终出现“ System.Data.Entity.Core.EntityCommandExecutionException”,然后出现内部异常:“ SqlException:无效的列名'Person_Id'。”

“我的护士”模型中没有“ Person_Id”字段。生成的查询似乎已经添加了它。我必须在模型定义中缺少某些内容,因为删除“ public virtual Person Person {get; set;}”会使查询正确生成。 EF似乎对PK在“人”表中为Id,但在“护士”表中为NurseId感到困惑。

我在互联网上找到了多个地方,这些地方向您展示了如何使用Fluent API进行映射,但没有任何一个案例与我相符。因为我的应用程序中的大多数Person都不是Nurse,所以我不想在Person模型中引用Nurse模型,这取消了我一直在网上看到的解决方案:

//won't work because I am trying to no have a reference to Nurse in my Person model.  So i.Nurse below doesn't exist
modelBuilder.Entity<Nurse>().HasRequired(i => i.Person).WithOptional(i => i.Nurse) 

查询EF生成:

{SELECT 
1 AS [C1], 
[Extent1].[NurseId] AS [NurseId], 
[Extent1].[CredentialId] AS [CredentialId], 
[Extent1].[DisciplineId] AS [DisciplineId], 
[Extent1].[UnitId] AS [UnitId], 
[Extent1].[YearsOfService] AS [YearsOfService], 
[Extent1].[Person_Id] AS [Person_Id]
FROM [dbo].[Nurse] AS [Extent1]}

以下是表格:

CREATE TABLE [dbo].[Person]
(
    [Id] BIGINT IDENTITY (1, 1) NOT NULL,
    CONSTRAINT [PK__Person] PRIMARY KEY CLUSTERED ([Id] ASC)
)

CREATE TABLE [dbo].[Nurse]
(
    [NurseId] BIGINT NOT NULL, 
    CONSTRAINT [PK_Nurse] PRIMARY KEY ([NurseId]),
    CONSTRAINT [FK_Nurse_Person] FOREIGN KEY ([PersonId]) REFERENCES [dbo].[Person] ([Id])
)

实体:

public class Person 
{
    [Key]
    public long Id { get; set; }
}

public class Nurse
{
    [Key]
    public long NurseId { get; set; }

    public long? CredentialId { get; set; }

    public long? DisciplineId { get; set; }

    public long? UnitId { get; set; }

    public int? YearsOfService { get; set; }

    [ForeignKey("PersonId")]
    public virtual Person Person { get; set; }

    [ForeignKey("CredentialId")]
    public virtual Credential Credential { get; set; }

    [ForeignKey("DisciplineId")]
    public virtual Discipline Discipline { get; set; }

    [ForeignKey("UnitId")]
    public virtual Unit Unit { get; set; }
}

ApplicationDBContext:

public class ApplicationDBContext : DbContext
{
    public ApplicationDBContext() : base("ApplicationDbContext")
    {

    }

    public DbSet<Nurse> Nurses { get; set; }
    public DbSet<Person> People { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
       modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
       modelBuilder.Entity<Nurse>().HasKey(n => n.NurseId);
    }
}

是否可以执行此操作,还是需要在“人员”表中引用“护士”并使用我在网上看到的内容?

2 个答案:

答案 0 :(得分:0)

我认为您的问题是您使用的PersonId在Nurse中具有主键。尝试给护士自己的ID。每个表都有自己的ID是一个好主意。 EF对于没有唯一主键的表或视图存在问题。

    CREATE TABLE [dbo].[Person]
(
    [Id] BIGINT IDENTITY (1, 1) NOT NULL,
    CONSTRAINT [PK__Person] PRIMARY KEY CLUSTERED ([Id] ASC)
)

CREATE TABLE [dbo].[Nurse]
(
    [Id] BIGINT IDENTITY (1, 1) NOT NULL,
    [PersonId] BIGINT unique NOT NULL,  
    CONSTRAINT [PK_Nurse] PRIMARY KEY ([Id]),
    CONSTRAINT [FK_Nurse_Person] FOREIGN KEY ([PersonId]) REFERENCES [dbo].[Person] ([Id])
)

答案 1 :(得分:0)

对于那些偶然发现的人。我找到了答案here。我发布的流利的api已经关闭,应该是:

modelBuilder.Entity<Nurse>().HasRequired(n => n.Person).WithOptional();