简体   繁体   中英

Entity Framework Code First Composite Foreign Key on legacy DB

So, I've been banging my head for a few days now and searching has led me nowhere close to a solution.

I have a legacy database thats far from following EF's code convention and I'm using EF Code First.

This is my actual situation: (Irrelevant fields ommited for the sake of brevity)

[Table("PEDIDO")]
public class Pedido : IValidatableObject
{
    [Key, Column(Order = 1), Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CODIGO { get; set; }

    [Key, Column(Order = 2), ForeignKey("DadosCliente"), Required]
    public int CLIENTE { get; set; }
    public virtual Cliente DadosCliente { get; set; }

    public virtual ICollection<MaterialPedido> DadosMateriaisPedido { get; set; }

}


[Table("MATERIAL_PEDIDO")]
public class MaterialPedido : IValidatableObject
{
    [Key, Required, Column(Order = 1)]
    public int NRPEDIDO { get; set; } // This column relates to Pedido.CODIGO

    [Key, Required, Column(Order = 2), ForeignKey("DadosCliente")]
    public int CLIENTE { get; set; }
    public virtual Cliente DadosCliente { get; set; }

    [Key, Required, Column(Order = 3)]
    public string CODIGO { get; set; }
    // Please note that this column is some sort of "virtual field". Its value should be hard-coded to "P" when relating to the table "PEDIDO"
}

public class EntitiesContext : DbContext
{
     protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Pedido>()
            .HasKey(p => new { p.CLIENTE, p.CODIGO })
            .HasMany(x => x.DadosMateriaisPedido)
            .WithOptional(p => p.Pedido)
            .HasForeignKey(x => new { x.CLIENTE, x.NRPEDIDO })
            .WillCascadeOnDelete(false);
    }
}

As of now I'm getting the following error:

ProjetoPI.EF.Pedido_DadosMateriaisPedido: : Multiplicity conflicts with the referential constraint in Role 'Pedido_DadosMateriaisPedido_Source' in relationship 'Pedido_DadosMateriaisPedido'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'. Pedido_DadosMateriaisPedido_Target: : Multiplicity is not valid in Role 'Pedido_DadosMateriaisPedido_Target' in relationship 'Pedido_DadosMateriaisPedido'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.

What am I missing?Any help will be greatly appreciated!

So, I've finally found out what was happening so below is my solution for future reference and hoping it's able to help other desperate souls like me:

My mapping was entirely wrong. MATERIAL_PEDIDO has no relationship with PEDIDO (not sure I agree with this design, but...) and Entity Framework does not approve the creation of relationships that do not exist on the database. There is a third table that, besides containing MaterialPedido's children, its the relationship between Pedido and MaterialPedido.

[I can't post images, so I won't be able to post the DB Model =(]. So I cleared all relationships and started remodeling from scratch.

Here are my working relationships:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Pedido>()
            .HasKey(p => new { p.CLIENTE, p.CODIGO })
            .HasMany<DetalhePedido>(p => p.DetalhesPedido)
            .WithRequired(dp => dp.Pedido)
            .HasForeignKey(dp => new { dp.CLIENTE, dp.NRPEDIDO });

        modelBuilder.Entity<MaterialPedido>()
            .HasKey(mp => new { mp.NRPEDIDO, mp.CLIENTE, mp.CODIGO })
            .HasMany<DetalhePedido>(mp => mp.DetalhesPedido)
            .WithRequired(dp => dp.MaterialPedido)
            .HasForeignKey(dp => new { dp.NRPEDIDO, dp.CLIENTE, dp.MATERIAL });
    }

[Table("MATERIAL_PEDIDO")]
public class MaterialPedido : IValidatableObject
{
    [Key, Required, Column(Order = 1)]
    public int NRPEDIDO { get; set; }

    [Key, Required, Column(Order = 2), ForeignKey("DadosCliente")]
    public int CLIENTE { get; set; }
    public virtual Cliente DadosCliente { get; set; }

    [Key, Column(Order = 3)]
    public string CODIGO { get; set; }

    public virtual ICollection<DetalhePedido> DetalhesPedido { get; set; }

}

[Table("DETALHE_PEDIDO")]
public class DetalhePedido
{
    [Key, Column(Order = 1)]
    public int NRPEDIDO { get; set; }
    [Key, Column(Order = 2)]
    public int CLIENTE { get; set; }
    [Key, Column(Order = 3)]
    public string MATERIAL { get; set; }
    [Key, Column(Order = 4)]
    public string CODIGO { get; set; }

    public virtual Pedido Pedido { get; set; }
    public virtual MaterialPedido MaterialPedido { get; set; }
}

[Table("PEDIDO")]
public class Pedido : IValidatableObject
{
    [Key, Column(Order = 1), DisplayName("Cód Pedido"), Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CODIGO { get; set; }

    [Key, Column(Order = 2), Required, CustomValidation(typeof(GranitoEntities), "NotZero")]
    public int CLIENTE { get; set; }
    [ForeignKey("CLIENTE")]
    public virtual Cliente DadosCliente { get; set; }

    public virtual ICollection<DetalhePedido> DetalhesPedido { get; set; }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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