hidden
hidden

Reputation: 3236

Very simple dillema that has been killing me scaffolding in mvc 3

Summary of the question: How do i scaffold two or more tables with linq to entities.

I cant find an example; they always scaffold only one table.

Details:

If I have two tables and I use LINQ to entities with a t4 template for dbcontext capability like such:

Table1 
Name    LastName  PositionId
Jose     j        1

Table2 
PositionPrimaryKey   PositionId   PositionDescription
1                      1             MainProgrammer

If I had these table mapped with linq to entities how would I scaffold them?

Then i put Table1 as my Model class. I have my employeesentities as dbcontext

But that only creates the values for table 1 and not 2.

If I create a new model that contains both entities, it says is not part of Employeeentities and the class could not be modfied to add my new entity.

Upvotes: 1

Views: 82

Answers (1)

tec-goblin
tec-goblin

Reputation: 316

So, you have a 1:1 relationship between these tables? If yes, I suggest you creating an entity by hand, set its defining query to the necessary join, and map Insert/Update/Delete to stored procedures in the Mapping Details screen. It involves some (quite simple) sql, but it is the cleanest way for your code above.

If it's not a 1:1 relationship, you need to modify the t4 template to conditionally create the fields of the linked property (it has to navigate the property, and based on some condition, like you say "if property is called Table2", create the extra fields). If you have already done so and it doesn't work, maybe there's something going on with the selection of properties used by MVC scaffolding. It might use reflection and choose only primitive types.

Upvotes: 1

Related Questions