Dylan Hayes
Dylan Hayes

Reputation: 2366

Entity Framework - Including tables not mapped in data model?

I think this question is probably fairly simple, but I've been searching around and haven't been able to find what I'm looking for.

My team and I are adding a new module to our existing web application. We already have an existing data model which is hooked up to our sql db, and it's pretty huge... So for the new module I created a new EF data model directly from our database with the new tables for the new module. These new tables reference some of our existing tables via foreign keys, but when i add those tables, all of the foreign keys need to be mapped for that table, and their tables, and their tables... and it seems like a huge mess.

My question is, instead of adding the old tables to the data model, since I'm only referencing the ID's of our existing tables for Foreign key purposes can I just do a .Includes("old table") somewhere in the DataContext class or should I go back and add those tables to the model and remove all of their relationships? Or maybe some other method I'm not even aware of?

Sorry for the lack of code, this is more of a logic issue rather than a specific syntax issue.

Upvotes: 1

Views: 2271

Answers (3)

Crab Bucket
Crab Bucket

Reputation: 6277

I'm not aware that you can use Include to reference tables outside of the EF diagram. To start working with EF then you only need to include a portion of the database in - if your first project is working with a discrete functional area which it probably would be. This might get round the alarming mess when you import and entire legacy database. It scared me when I tried to do it.

In our similar situation - a big legacy system that used stored procedures, we only added the tables that we were directly working at that time. Later on you can always add in additional tables as and when you require them. Don't worry about foreign keys in the EF diagram that are referencing tables that aren't included. Entity Framework happily copes with this.

It does mean running two business layers though one for entity framework and one for the old style data access. Not a problem for us though. In fact from what I've read about legacy system programming it's probably the way to go - you have a business layer with your scruffy old stuff and a business layer with your sparkly new stuff. Keep moving from old to the new until one day the old business layer evaporates into nothing.

Upvotes: 1

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364369

Simple answer is no. You cannot include entity which is not part of your model (= is not mapped in your EDMX used by your current context).

More complex answer is: in some very special case you can but it requires big changes to your development process and the way how you work with EF and EDMX. Are you ready to maintain all EDMX files manually as XML? In such case EF offers a way to reference whole conceptual model in another one and use one way relations from the new model to the old model. It is a cheat because you will have multiple conceptual models (CSDL) but single mapping file (MSL), single storage description (SSDL) and single context using all of them. Check this article for an example.

Upvotes: 2

NoWar
NoWar

Reputation: 37632

You have to use [Include()] over the member.

For example:

        // This class allows you to attach custom attributes to properties
        // of the Frame class.
        //
        // For example, the following marks the Xyz property as a
        // required property and specifies the format for valid values:
        //    [Required]
        //    [RegularExpression("[A-Z][A-Za-z0-9]*")]
        //    [StringLength(32)]
        //    public string Xyz { get; set; }
        internal sealed class FrameMetadata
        {

            // Metadata classes are not meant to be instantiated.
            private FrameMetadata()
            {
            }
            [Include()]
            public EntityCollection<EventFrame> EventFrames { get; set; }

            public Nullable<int> Height { get; set; }

            public Guid ID { get; set; }

            public Layout Layout { get; set; }

            public Nullable<Guid> LayoutID { get; set; }

            public Nullable<int> Left { get; set; }

            public string Name { get; set; }

            public Nullable<int> Top { get; set; }

            public Nullable<int> Width { get; set; }
        }
    }

And the LINQ should have

.Includes("BaseTable.IncludedTable")

syntax.

And for the entities which are not part of your model you have to create some view classes.

Upvotes: 0

Related Questions