简体   繁体   English

实体框架代码优先的一对一关系

[英]One to One relationship in Entity Framework Code First

I have a parent table as CustomerRequest that has a relationship with Loan table as 1 to 1-0, that means customer request can have 1 or 0 Loan in Loan Table. 我有一个作为CustomerRequest的父表,该表与贷款表之间的关系为1到1-0,这意味着客户请求在贷款表中可以有1或0个贷款。 I have problem in getting a correct relationship in Entity Framework code first. 我首先在实体框架代码中获得正确的关系时遇到问题。

This is My CustomerRequest Table Script with correct relationship: 这是具有正确关系的“我的CustomerRequest表”脚本:

CREATE TABLE [Loan].[CustomerRequest]
(
 [Id]               INT IDENTITY(1,1)
,[CorrelationId]    VARCHAR(500)
,[CustomerNumber]   BIGINT
....

 CONSTRAINT PK_CustomerRequest PRIMARY KEY([Id])
)

this is loan table: 这是贷款表:

 CREATE TABLE [Loan].[Loan]
 (
   [Id]                             INT    IDENTITY(1,1)
   ,[CustomerRequestId]             INT
   .....
 CONSTRAINT PK_Loan PRIMARY KEY([Id])
   ,CONSTRAINT FK_Loan_CustomerRequestId FOREIGN KEY([CustomerRequestId])  REFERENCES [Loan].[CustomerRequest]([Id])
 )

This is my Customer Request Model: 这是我的客户请求模型:

  public class CustomerRequest
{
    public int Id { get; set; }
    public string CorrelationId { get; set; }
    .....        
    public virtual Loan Loan { get; set; }
}

and Loan Model: 和贷款模型:

  public class Loan
   {
    public int Id { get; set; }
    public int CustomerRequestId { get; set; }
    ....
    public virtual CustomerRequest CustomerRequest { get; set; }
      }

I have this relationship in CustomerRequestMap: 我在CustomerRequestMap中具有以下关系:

    HasKey(t => t.Id).HasOptional(t => t.Loan).WithRequired(t => t.CustomerRequest);

I get this error when I try to insert to Loan table: 当我尝试插入贷款表时出现此错误:

 {"Cannot insert explicit value for identity column in table 'Loan' when IDENTITY_INSERT is set to OFF."}

You have to modify your entities and configurations a bit like this. 您必须像这样修改您的实体和配置。

public class CustomerRequest
{
    public int Id { get; set; }
    public string CorrelationId { get; set; }
    public virtual Loan Loan { get; set; }
}

public class CustomerRequestMap : EntityTypeConfiguration<CustomerRequest>
{
    public CustomerRequestMap()
    {
        Property(x => x.Id)
            .IsRequired()
            .HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
    }
}

public class Loan
{
    //public int Id { get; set; }
    [Key, ForeignKey("CustomerRequest")]
    public int CustomerRequestId { get; set; }
    public virtual CustomerRequest CustomerRequest { get; set; }
}

public class LoanMap : EntityTypeConfiguration<Loan>
{
    public LoanMap()
    {
        HasRequired(m => m.CustomerRequest)
        .WithOptional(m => m.Loan)
        ;
    }
}

With this configuration you can have 1 - 0 or 1 relation but you have to modify your schema script. 使用此配置,您可以具有1-0或1关系,但是您必须修改架构脚本。

You can't have Id as primary key and CustomerRequestId as foreign key, EF does not allow this in 1-0 or 1 relation. 您不能将Id作为主键,而将CustomerRequestId作为外键,EF不允许1-0或1关系。 And it's a wrong design from schema point of view too. 从架构的角度来看,这也是一个错误的设计。

Imagine the situation when you have CustomerRequest with Id=1 . 想象一下当您的CustomerRequest Id=1 Then you insert two Loan entry with CustomerRequestId=1 but with different Id primary key. 然后,插入两个带有CustomerRequestId=1但具有不同Id主键的Loan条目。 How will you query the two Loan entry from EF model in 1-0 or 1 relation? 您将如何以1-0或1关系查询EF模型中的两个Loan条目?

If the schema is fix and you can't modify it then you must go with 1-many relation. 如果该模式是固定的,并且您不能对其进行修改,那么您必须采用“一对多”关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM