Must I use all tables in an Entity Framework model?

I am building an Entity Framework model for a subset of the Pubs database from microsoft. I am only interested and publishers and books, not publishers and employees, but there is a foreign key constraint between the publishers and emoloyees tables. When I remove the employees entity from my model, the model won't validate because of the foreign key constraint.

How do I create a model for a subset of a database when that subset links to other tabes with foreign key constraints?

Because this is for a demo, I deleted the offending tables and constraints from the database, but this won't work in production.

Answers


The correct way to do this is by exposing the foreign key columns as scalar properties. There is a complete explanation, and downloadable sample code, in this blog post. You might find the rest of the post interesting, as well.


You could create views of the pertinent data and bind your model to that. I am not a database expert, but a DBA that I formerly worked with recommended this approach because she said that the view is less intensive on the database server to begin with.

Prior to the release of 3.5 SP1, we built a DAL on top of LINQ to SQL (without DBML mappings, but that is another story) that mapped all of the domain objects to either stored procedures or views. That way, the DBA was happy about the calls following a more set execution plan, as well as being able to encapsulate the database logic outside of the codebase.


Need Your Help

How pass data between two activities while background activity always running

android sharedpreferences handler background-process

So as illustration below. i want my background activity (stream data from bluetooth) places in class A always running whenever i change my screen to class B. The UI only update where the screen act...

Updating the model in the controller?

c# asp.net-mvc view controller

I'm using ASP.NET MVC 3. I have a question if it's possible to update the model, even if it's not being sent to the controller? Perhaps the question is completle of, or I'm doing things in a wrong ...