简体   繁体   English

如何首先在EF代码中创建多个一对一关系?

[英]How to create Multiple one-to-one relationships in EF Code first?

Third Edit: Changed plenty of my code at the end of the question, left the original just in case that was the way to go . 第三修改:在问题的末尾更改了很多代码,保留了原始代码,以防万一

I'm having a problem understanding how to work with multiple one-to-one relationships in EF. 我在理解如何在EF中处理多个一对一关系时遇到问题。 I already had made a one-to-one relationship between two models, but when I tried to add another model one-to-one relationship to the mix, things got ugly. 我已经在两个模型之间建立了一对一的关系,但是当我尝试向混合模型中添加另一个模型一对一的关系时,事情变得很糟。 Second Edit: I now am working with Composite keys, since there are another relationships in the database that seemed to use them. 第二次编辑:我现在正在使用复合键,因为数据库中似乎还有其他关系正在使用它们。 Still having problems doing the one-to-one relationships, here's the code: 仍然存在一对一关系的问题,下面是代码:

First, I got the one of the parents: 首先,我得到了一位父母:

public class CodigoAgrupadorCuentas
{
   [Key]
    [Column(Order = 0)]
    [StringLength(36)]
    public string CompanyID { get; set; }

    [Key]
    [Column(Order = 1)]
    [StringLength(36)]
    public string DivisionID { get; set; }

    [Key]
    [Column(Order = 2)]
    [StringLength(36)]
    public string DepartmentID { get; set; }

    [Key]
    [Column(Order = 3)]
    [Required]
    public int CodigoAgrupadorCuentasID { get; set; }

    [Required]
    public string Code { get; set; }

    public virtual CatalogoDeCuentas CatalogoDeCuentas { get; set; }
}

Here's the the child that has two parents: 这是有两个父母的孩子:

public class CatalogoDeCuentas 
{
    public CatalogoDeCuentas()
    { }
     [Key]
    [Column(Order = 0)]
    [StringLength(36)]
    public string CompanyID { get; set; }

    [Key]
    [Column(Order = 1)]
    [StringLength(36)]
    public string DivisionID { get; set; }

    [Key]
    [Column(Order = 2)]
    [StringLength(36)]
    public string DepartmentID { get; set; }

    [Key]
    [Column(Order = 3)]
    [Required]
    public int CatalogoDeCuentasID { get; set; }

    public string Information { get; set; }

    public virtual CodigoAgrupadorCuentas CodigoAgrupadorCuentas { get; set; }

    public virtual LedgerChartOfAccount LedgerChartOfAccount { get; set; } 

}
}

Second Parent class: 二等班:

public partial class LedgerChartOfAccount
{
    [Key]
    [Column(Order = 0)]
    [StringLength(36)]
    public string CompanyID { get; set; }

    [Key]
    [Column(Order = 1)]
    [StringLength(36)]
    public string DivisionID { get; set; }

    [Key]
    [Column(Order = 2)]
    [StringLength(36)]
    public string DepartmentID { get; set; }

    [Key]
    [Column(Order = 3)]
    [StringLength(36)]
    public string GLAccountNumber { get; set; }

    public string description { get; set; }


    public virtual CatalogoDeCuentas CatalogoDeCuentas { get; set; }
}

And finally, here's the context: 最后,这是上下文:

public class PolizasDBContext: DbContext
{
      public PolizasDBContext()
        : base("PolizasDBContext")
    { 

    }

      public DbSet<CatalogoDeCuentas> TablaCatalogoDeCuentas { get; set; }
      public DbSet<CodigoAgrupadorCuentas> TablaCodigoAgrupCuentas { get; set; }
      public DbSet<LedgerChartOfAccount> TablaChartAccounts { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
       modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<LedgerChartOfAccount>()
          .HasKey(x => new { x.CompanyID, x.DivisionID, x.DepartmentID, x.GLAccountNumber });

        modelBuilder.Entity<CatalogoDeCuentas>()
            .HasKey(x => new { x.CatalogoDeCuentasID, x.CompanyID, x.DivisionID, x.DepartmentID });

        modelBuilder.Entity<CodigoAgrupadorCuentas>()
            .HasKey(x => new { x.CodigoAgrupadorCuentasID, x.CompanyID, x.DivisionID, x.DepartmentID });

        modelBuilder.Entity<CatalogoDeCuentas>()
            .HasRequired(x => x.CodigoAgrupadorCuentas)
            .WithOptional(x => x.CatalogoDeCuentas)
            .Map( x => x.MapKey("CompanyID"));

        modelBuilder.Entity<CatalogoDeCuentas>()
           .HasRequired(x => x.LedgerChartOfAccounts)
           .WithOptional(x => x.CatalogoDeCuentas)
           .Map(x => x.MapKey("CompanyID"));

        base.OnModelCreating(modelBuilder);

    }

At this point, I get the next error when I try to scaffold: 在这一点上,当我尝试脚手架时,我得到下一个错误:

脚手架错误

In Summary: What would be the way, to keep a one to one relationship between CodigoAgrupadorCuentas and CatalogoDeCuentas, and at the same time have a one to one relationship between LedgerChartOfAccount and Catalogo de Cuentas? 简介:如何保持CodigoAgrupadorCuentas和CatalogoDeCuentas之间的一对一关系,同时在LedgerChartOfAccount和Catalogo de Cuentas之间保持一对一的关系?

First Edit: Forgot to mention, that GLAccountnumber was suppose to be LedgerChartOfAccounts Key. 第一次编辑:忘记提及了,GLAccountnumber应该是LedgerChartOfAccounts键。

Third Edit: 第三编辑:

Still trying to fix this, and I honestly have no idea why i'm struggling so much, from all the research I have done it should be simple. 仍在尝试解决此问题,老实说,我不知道为什么我要为此付出如此多的努力,从我所做的所有研究来看,它应该很简单。 I have made again plenty of changes to the code above. 我再次对上面的代码进行了大量更改。 Here's the changes: 这是更改:

 public class CodigoAgrupadorCuentas
{
    [Key]
    [Required]
    public int CodigoAgrupadorCuentasID { get; set; }

    [Required]
    public string Code { get; set; }

    public CatalogoDeCuentas CatalogoDeCuentas { get; set; }
}

Removed the Composite keys from Codigo Agrupador, since from what I read, I don't necessarily need them to make this work. 从Codigo Agrupador中删除了Composite键,因为从我阅读的内容来看,我不一定需要它们来完成这项工作。 Also removed the virtual keyword virtual from CatalogoDeCuentas, because of an error I had that i'll show at the end. 还从CatalogoDeCuentas中删除了虚拟关键字virtual,这是由于我将在最后显示的错误所致。 But anyways, I also changed: 但是无论如何,我也改变了:

public class CatalogoDeCuentas 
{
    public CatalogoDeCuentas()
    { }

    [Key]
    [Column(Order = 0)]
    [Required]
    public int CatalogoDeCuentasID { get; set; }

    [Key]
    [Column(Order = 1)]
    [StringLength(36)]
    [Required]
    public string GLAccountNumber { get; set; }

    public string Information { get; set; }

    public CodigoAgrupadorCuentas CodigoAgrupadorCuentas { get; set; }

    public LedgerChartOfAccount LedgerChartOfAccount { get; set; } 

}
}

Almost same changes, only that in this case, I wanted to link CatalogoDeCuentas and LedgerAccountOfChart with the GLAccountNumber, that's why I added it as a key. 几乎相同的更改,只是在这种情况下,我想将CatalogoDeCuentas和LedgerAccountOfChart与GLAccountNumber链接,这就是为什么将它添加为键的原因。

Another big change was on the Context: 上下文中的另一个重大变化是:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<CatalogoDeCuentas>()
            .HasRequired(x => x.CodigoAgrupadorCuentas)
            .WithOptional(x => x.CatalogoDeCuentas);

        modelBuilder.Entity<CatalogoDeCuentas>()
           .HasRequired(x => x.LedgerChartOfAccounts)
           .WithOptional(x => x.CatalogoDeCuentas);

        base.OnModelCreating(modelBuilder);

    }

Made it a lot simpler. 简化了很多。 The only change I did to LedgerChartOfAccounts from the original code posted was removing the virtual word in CatalogoDeCuentas. 我对发布的原始代码对LedgerChartOfAccounts所做的唯一更改是删除了CatalogoDeCuentas中的虚拟单词。

Now, when I try to scaffold, I get the next error: 现在,当我尝试脚手架时,出现下一个错误:

新脚手架错误

And that's where I am so far... help? 到目前为止,这就是我的...帮助吗?

For it to be (One-to-One) LedgerChartOfAccount should have a primary key of public int CatalogoDeCuentasID {get; set;} 要使其成为(一对一), LedgerChartOfAccount应该具有public int CatalogoDeCuentasID {get; set;} public int CatalogoDeCuentasID {get; set;}

In the model builder your configuration should look like this: 在模型构建器中,您的配置应如下所示:

modelBuilder.Entity<LedgerChartOfAccount>()
                   .HasKey(x => x.CatalogoDeCuentasID);

modelBuilder.Entity<LedgerChartOfAccount>()
            .HasRequiredPrincipal(x => x.CatalogoDeCuentas)
            .WithRequired(x => x.LedgerChartOfAccount);

For more information check out Configuring Relationships with the Fluent API 有关更多信息,请查看使用Fluent API配置关系。

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

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