TechQuery
TechQuery

Reputation: 253

Entity Framework Multiple EDMX file sharing same tables

Environment : EF 6, SQL 2012

Setup: Database First, LazyLoading disabled

The question might appear more generic but will try to explain it in the best possible way.

I have a large application using ASP.NET MVC and grouped the entity based on the logical functionality. Hence we built multiple EDMX files

There is scenario in which we have to use the similar entity in two EDMX file.

School has relation to Teachers and Students.In first EDMX file, i used school and Teachers.In Second EDMX file, i used school and students

But only one Entity class getting created. If i run the custom tool on second EDMX context file, then the entity(school.cs) on my first edmx getting disappeared and it appears on the second one..

Here is the code in my first EDMX file

As you see here, i m not accessing school entity and also i disabled Lazyloading. But it complains that it couldnt find school file. Note: Courses has navigation property to school. But i didnt include it here.. Why its occuring so?

 var courses= DB.courses
                                        .AsNoTracking()
                                        .Select(e =>
                                            new CourseDTO()
                                            {
                                                CourseID= e.CourseID,
                                                Name= e.CourseName,
                                                Desc= e.Desc,
                                                isActive= e.isActive
                                            })
                                        .OrderBy(e => e.CourseID);

The problem is, I m able to include one entity in the EDMX file only.. In first EDMX, it has navigation property to Teachers In second EDMX , it has navigation property related to Students. But only Entity file exists at a time.. With only one Entity file, the code breaks Note: This is just sample..Not my original application

Upvotes: 0

Views: 1390

Answers (1)

TechQuery
TechQuery

Reputation: 253

Thanks @GertArnold. Meanwhile, i tried to create folders and kept the EDMX file inside it. Means i created seperate folder for each logical group and then included edmx file inside it. This in turn made the edmx file entities have different name space(i mean the entity classes) and also it enabled to have the same entity across multiple EDMX files. It sounds to have resolved my problem.

I didnt try to have them included under different namespace.. The whole idea started when i realized that even though i have two EDMX files, the associated entities(.csfiles) are created in the same physical location. I tried to create sub folders and included the EDMX files. It resolved the problem and i found it is having different name space

:):)

Upvotes: 1

Related Questions