Table does not appear in EDMX models

7

I have a problem in an application that I'm developing, I created the templates using the ADO.NET Entity Data Model.

But a specific table is not showing up below the script I'm using to create the table, it's created and it works but no template is created for it.

SQL script that I'm using to create the table

-- -----------------------------------------------------
-- Table tb_r_veiculo_adicionais
-- -----------------------------------------------------
CREATE TABLE  tb_r_veiculo_adicionais (
  veiculo INT NOT NULL,
  adicional INT NOT NULL,
  PRIMARY KEY (veiculo, adicional),
  FOREIGN KEY (veiculo) REFERENCES tb_veiculo (codigo),
  FOREIGN KEY (adicional) REFERENCES tb_adicionais (codigo)
)

The error returned is below

  

Error 1 Error 3003: Problem in mapping fragments starting at line 528: Given the cardinality of Association End Member tb_veiculo, it should be mapped to key columns of the table tb_r_veiculo_adicional. Either fix the mapping or change the multiplicity of this end.   C: \ Users \ Hiago \ documents \ visual studio 2013 \ Projects \ ProjectJonathan \ ProjectJonathan \ project_jonathan.edmx 529 11 Jonathan Project

Note: When you delete EDMX and create works, only the template of the tb_r_veiculo_adicionais table is not generated.

UPDATE : I noticed that the problem only occurs when I create the foreign keys.

    
asked by anonymous 15.11.2015 / 17:53

2 answers

2

Tables such as tb_r_veiculo_adicionais are called relationship tables. Basically, this type of table has no business meaning, they are just a way to model n-to-m relationships in the relational world.

In the Object Oriented world you do not need a support entity to represent that relationship. Just that Veiculo has an additional collection and Adicional has a collection of vehicles :

public class Veiculo
{
   public int VeiculoId { get; set; }
   // demais propriedades
   public virtual ICollection <Adicional> Adicionais { get; set; }
}  

And the mapping:

modelBuilder.Entity<Veiulo>()
            .HasMany(v => v.Adicionais)
            .WithMany()
            .Map(x =>
            {
                x.MapLeftKey("veiculo");
                x.MapRightKey("adicional");
                x.ToTable("tb_r_veiculo_adicionais");
            });

This is one of many examples where the relational model and the object-oriented model do not match 100%. In English the technical term for these differences and mapping problems is object-relational impedance mismatch >.

By the time you ask Visual Studio to engineer your database, it defaults to the strategy of hiding the relationship table. This is not a problem, it's a feature .

Nothing prevents you from editing the generated template to include an explicit relationship entity if it is required (for example, if there are relationship attributes in the tb_r_veiculo_adicionais table). You can always edit your features manually. See this example in SOen on how to do this.

In addition, as other responses have pointed out, there are many ways to "force" the creation of an entity to represent a relationship table during the reverse engineering process of the bank (if for some reason you do not want to edit the relationship model manually). An option other than those that have already been pointed out is to add a temporary column to the tb_r_veiculo_adicionais table to force reverse engineering of it. After doing the reserve engineering of the bank you can delete the temporary column and make a refresh of the model (see SOen response for more details).

    
14.11.2016 / 14:08
-1

I've had this problem. I solved by creating a column in the table named Id of type int, as the primary key with Identity. The EF is lost with composite keys. Take the test.

    
16.11.2015 / 14:02