Friday, May 3, 2019

EF Code First: Designing models based on existing tables that do not have FK relationships defined

Having some trouble determining how I should handle a particular situation I've encountered at work.

Some backstory: I'm a jack of all trades type (SSRS, SSIS, SQL, C#) at a small consulting company. A few months ago I was tasked with a project which started out as a report and quickly turned into a full blown web app. I cobbled together some mess of an app that worked but was becoming a headache to maintain. Its an airport terminal style app that iterates through and displays a group of records for our customer to put up on a big tv in their warehouse (and includes an interface for their users to manage what records are displayed on that TV etc).

Initially, I created views for pretty much anything I needed to display and was using stored procedures to update records etc -- it is/was a real mess. Over the past couple days I've had some extra time so I started trying to rebuild it to be cleaner and more easily maintainable. In my rebuild, I am trying to use POCO objects to represent each of the tables that I was previously aggregating into views, so that I can perform all logic in the app (instead of having some split out into the view). The problem I'm now having is the tables I'm creating my models for do no have actual foreign key relationships defined.

If for example I have the following tables: Order and OrderItems. Each record in OrderItems belongs to a record in Order. If I were to write a query to get the data for an order, it would look like select * from Order inner join OrderItems on Order.OrderNumber = OrderItems.OrderNumber and Order.OrderType = OrderItems.OrderType.

Since I can't alter the tables and create foreign key relationships without potentially blowing up the primary app that uses them, I was wondering if there is a way to specify relationships in EF where it would not look for or attempt to alter the existing tables. Or if perhaps there is a better way to try to handle this entirely.

Thanks

EF Code First: Designing models based on existing tables that do not have FK relationships defined Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team