EF Power Tools- Sequence contains no matching element - entity-framework

My Solution consists of a different projects. I have a project for each of the contexts and project for a Base Context that all other context classes inherit for only referencing 1 database.
I have 3 other projects for the different Domain Classes(Customer, Rental, Owner) that have the actual entities. Also I have 1 helper class that the Domain Classes have reference too.
My problem is when I right click on any of the context classes and select Entity Framework>View Entity Data Model(Read Only) I get the "Sequence contains no matching element".
Now I have seen a couple of posts about this being a issue if the project actually was within a Solution Folder and as soon as this has been placed on the Solution root it starts working. In my case it is a project on the root directory of the Solution. This is my first project using Entity Framework, so if I am being vague I apologize, I have not seen any solution for my problem so any help would be appreciated.

I wrestled with this today as well. I also have separate projects for the domain objects (entities) and the EF model under solution folders. I finally got it working by first moving the project that contains the DbContext derived class and all its dependent projects out of the solution folder into the root.
Secondly, I needed to drop the default database that EF uses to create the model and it's metadata. It seems that when the power tool runs, it uses the default constructor of the class deriving from DbContext. In my case, that model was way out of date. I typically use an explicit connection string, so the default model hadn't been updated in a while. By default, EF will create a DB on the .\SQLEXPRESS instance, naming the DB after the class derived from DbContext.
Hope this helps,
Jeff.

It may be as simple as having the SetInitializer on database being set to null, and EF needing to upgrade your DB.
Either in code:
Database.SetInitializer<TContext>(null);
or in config
<appSettings>
<add key="DatabaseInitializerForType Namespace.Class, Assembly" value="Disabled" />
</appSettings>
Obviously the config is an assembly reference, the important bit is the 'value="Disabled"~.
This is normal practice so that EF doesn't accidentally drop your database when starting your app.

Related

Trying out Entity Framework Code-first; is the usage any different from Database-first?

When working with EF (v4,5,6) I have always used Database-first (I was mistakenly under the impression this was the only way to generate Entities from existing tables, EDMX, etc). But today I tryed Code-first, and it can also generate the POCOs (in a different way, no EDMX, different connection string, less cr8p lying around, etc..!)
So far, the usage of EF for CRUD appears to be exactly the same, can anyone who has used both please confirm there is nothing different (in usage), or gotchas I should be aware of?
And a supplementary question is, can I generate both in the same project ? (Not that i want to, but existing proj has EDMX within a folder, can I create another folder and generate Code-First Entities (different set of tables only), so i end up with DBContext and DBContext2 ?
Yes, the usage is the same. If you check the generated code you'll see they use the same System.Data.Entity.DbSet properties and they both inherit from the same System.Data.Entity.DbContext class.
Yes, you can generate both in the same project, but does not makes much sense, because you have to maintain both of them if the DB changes.

EF6 How to avoid creating tables for entities managed by another DbContext?

I have below structure of classes:
SecurityLayer.Domain dll
User class and some other classes
Has a nuget package for this
SecurityLayer.Data dll
SecurityDbContext class (which inherits from DbContext and manages User related entities
Has a nuget package for this
Now I build another product which uses two packages above. And I have below structure
MyProduct.Data dll has a ProductDbContext to manage entities from MyProduct.Domain dll
The problem is when I use Add-Migration on MyProduct.Data project, EF generates a migration to create ALL entities from both MyProduct.Domain and SecurityLayer.Domain
I know that EF6 support multiple DbContext in a single database, but it does not work for me. Can we do something to let EF avoid entities in SecurityLayer.Domain? Any advice is much appreciate. Tks a lot!
The easiest way would be to create your initial migration and remove the bits from it that are not relevant. Subsequent migrations will not pick up the existing tables due to the way EF works.
Migrations actually store the state of the database in a table called __MigrationHistory. One of those columns contains a zipped up EDMX file. You can extract the content as a blob, save it as a .zip file and see for yourself. Each migration uses that to determine what has changed since the previous migration.

Entity name conflicts in Visual Studio

I'm new to the Entity Framework. I've created a model-first schema in Visual Studio 2012 with EF5, but I'm having trouble getting the generated code to build. One of my entities ("Test") has the same name as the model (edmx) and the project (csproj) files, so it conflicts with the namespace (Test is a namespace but is used like a type). So I decided to change the namespace by setting a "Custom Tool Namespace" for the .tt files. This worked, but then I found that the "Test" entity's generated .cs file was entirely empty (other entities were generating properly), so I had build errors where other entities reference "Test". Renaming the entity results in a properly generated class, and therefore a building project, but I really want to use the original name.
I will probably end up scratching the project and starting over, ensuring to choose unique names for the project, the model, and the entity. But I'd rather know how to fix this if possible in case I run into something similar when the project is further along and it's not so easy to start over.
you can use use an alias on the Using Directive, e.g. using Project = PC.MyCompany.Project; to differentiate between namespaces. see MSDN

Multiple DbContexts in a single project

I am working on a project using EF5 and am lost when it comes to adding a second DbContext to the project. I have created the DbContext but my migrations do not work. How do I make sure that my second DbContext (totally separate database) is set up and I can start updating the database?
Doing a "Enable-Migration" fails because a Configuration file/class already exists for my previous DbContext.
You should split your data access libraries into separate projects. This way you can treat each one separately, and they have no danger of conflicting with each other (so long as you call them something different). I would also suggest making sure they are in unique namespaces.

Entity Frame work Coded first - Create Readonly EDMX Model

I am just starting out with EF Code-first:
If I create a read-only EDMX file from my code first can I save this readonly file as a stand-alone file and then include the resultant file in my project without affecting code first implementation.
Even though I am doing code first, I like a picture as it helps when I am explaining the db model to others. But I do not want to included this if it effects the code-first approach
will adding the EDMX file to the code-first project cause problems with the code-first approach?
No it doesn't have any effect on your code First approach. You can add the .edmx file directly to your project from Add-> New Item and generate the model from database without affecting your code-first approach. Just remember to use different namespaces so you don't get namespace conflict between your code-first and model classes.
To remove the Model, just remember to delete the related connection string in app.config/web.config to prevent your config files from becoming messy.