0

These are my first steps with the entity framework 6.2.0, target framework is 4.6.1.

I am using a "model first" approach, automatically generating the entity classes and DDL code for the database structure based on the EDMX model. For this model, the database generation Workflow "TablePerTypeStrategy.xaml (VS)" is used, and the generated DDL code shows many tables, as one would expect.

However, the entity classes in the generated code files beneath model.tt are missing the Table attribute, which, as i understand it, are mandatory for using TPT strategy. I had to manually add those to get my unit tests working.

Am i missing something? Or is the tt-file actually really lacking the code to generate that attribute?

Update: As requested, here a screenshot of (a part of) the model.

enter image description here

Update 2: This is the connection string used:

private const string ConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"P:\\Visual Studio\\Projects\\RightsManager\\Database\\RightsManager.mdf\"; Integrated Security=True;Connect Timeout=30";
Udontknow
  • 1,472
  • 12
  • 32
  • As far as I'm aware in a "model first" approach, EF doesn't specify Table attribute to entity classes. The Table attribute is used in a "code first" approach. What was the issue with your unit tests that made you believe you have to add Table attribute? – Claudiu Tomescu Sep 21 '18 at 18:32
  • Well, without it, a single table "Dbo.Entities" was used for persistence instead, containing one column for each property of each of my entities and a discriminator column ("Table Per Hierarchy"). The tables generated by the ddl were ignored and not filled. I'm still wondering which code generated this table in first place, it was not found in my Model.edmx.sql file... – Udontknow Sep 21 '18 at 18:38
  • Can you please post a screenshot of your EDMX? I'd like to see how those entities are defined – Claudiu Tomescu Sep 21 '18 at 18:46
  • The naming in the mapping details "EntitySet_RoleGroup" suggests that TPT is used. The entity RoleGroup is *not* mapped to a single table "Entities". – Gert Arnold Sep 22 '18 at 09:48
  • Yes, yes, you are right; but the behaviour of the DbContext descendant is still TPH as long as i don't add the Table attribute to the entity classes. Oh, i forgot to mention that i also had to override OnModelCreating in the context class and call "Database.SetInitializer(null);" to get it to work... – Udontknow Sep 22 '18 at 10:51
  • That means that you're working code-first with classes generated by the EDMX, not model-first. How did you generate the DDL you mention and what does the connections string look like? – Gert Arnold Sep 22 '18 at 14:53
  • Well, i just selected "Generate Database from Model" inside the EDMX model popup menu. I added the connection string to the question (don't think it helps, though). – Udontknow Sep 24 '18 at 09:01

0 Answers0