Table does not appear in EDMX models

Asked

Viewed 749 times

7

I have a problem in an application I am developing, I created the models using the ADO.NET Entity Data Model.

However a specific table is not appearing follows below the script I am using to create the table, it is created and works but is not created template for it.

SQL script 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)
)

Below is the error returned

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_adicionais. Either fix the Mapping or change the multiplicity of this end. C: Users Hiago Documents visual studio 2013 Projects Projectprojects Projectprojects

Observing: When deleting EDMX and creating works, only the table template tb_r_veiculo_adicionais is not generated.

UPDATING: I realized that the problem only occurs when I create the Foreign Keys.

  • vc defined the code as PK in the tb_vehicle table ?

  • Yes I did, if you want I put the whole SQL.

  • I don’t really remember now, but I think it’s because of the composite keys PRIMARY KEY (veiculo, adicional), try to put an Id in the table as PK in place of composite key.

  • @Marconciliosouza is a requirement I have the composite key, and the Foreign Keys... I can’t change them. Is there any other way?

  • Behold link

  • Comrade, this is because your table only has fields that are primary or foreign keys.

  • Yes, but can you explain to me why he does not accept the table under these conditions? Because they are normalized, they are not out of standard.

  • Ex: very simple, very simple. Look at the following code: db.TbMusicas.Find(Musi.Idmusica).TbEstilosMusica.Add(styli); Where: db = Entitymodel; Tbmusicas = Table name Mother; Musi.idMusica = Id of the value I look for in the music table; Tbestilosmusica = Name date mother table containing various music styles (Note: I have a daughter table of Tbmusicas that links to the Tbestilosmusica table. This so-called table is called Tbmusicas_styles); styli = is a value found in Tbestilosmusica; It seems a bit confusing, but it’s very simple. I wait for feedback.

  • yes more what would be the reason it does not generate an EDMX? Is there anything official about it? is a problem? is not it? am I doing wrong?

  • I already turned in the job, I just wanted to understand why it didn’t work with the FK

  • It’s no problem. It’s a class principle in Object-Oriented Programming. And EDMX (or Entity) works based on classes. When a class has only "foreign keys" as probabilities/variables, the way one should work with this class becomes somewhat different.

Show 6 more comments

2 answers

2

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

In the Object-Oriented world you don’t need a support entity to represent that relationship. It’s enough that Veiculo has a collection of additional 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.

The moment you ask Visual Studio to back up its database engineering, it adopts the default strategy of hiding the relationship table. That’s not a problem, it’s a Feature.

Nothing prevents you from editing the generated template to include an explicit relationship entity if needed (for example, if there are relationship attributes in the table tb_r_veiculo_adicionais). You can always edit your entities manually. See this example in Soen about 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 whatever reason, you don’t want to edit the model manually). An option other than those that have already been pointed out is to add a temporary column to the table tb_r_veiculo_adicionais to force reverse engineering of the same. After making the bank’s engineering reserve you can delete the temporary column and make a refresh of the model (see that reply from Soen for more details).

-1

I already had this problem. I solved creating a column in the table called Id of type int, as primary key with Identity. The RU gets lost with composite keys. Take the test.

  • André Monteiro, I would like to know why with the composite key does not create. In this case you have to do the project with the database provided by the university. =/

  • Hiago, send the scripts of the foreign tables (tb_vehicle and tb_additional) that I will perform the tests.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.