EF 4.3.1 code first table index mapping or creation - entity-framework

I've been searchin around a lot for a way to create, or configure a specific index on a table from EF code first, but really can't find a thing. mapping fluent Api doesn't seem to expose any means to do this. What am I missing?

You can add index creation either to custom database intitializer as mentioned in linked answer or you can use code based migrations. Migrations provide support for index creation.

Related

How do I ensure a table exists in EF Core without using migrations?

I'm writing a quick prototype project. I don't want to use EF migrations because I don't really understand them yet. The app imports data from a file into a DB. If the schema changes, my strategy will be "drop the tables and let EF rebuild them".
This works OK for adding data at startup, but I had trouble deciding if I needed to build the DB. For example:
var inputCount = parsedData.Items.Length;
var dbCount = _itemsContext.Items.Count();
if (inputCount != dbCount)
{
// Do updates/inserts
}
Since the table doesn't exist, I can't query for a count. I could handle the exception but that felt a little messy. I just needed EF to create the table if it's not there. Everything I read walked me through adding migrations to the project but it feels like a feature I didn't want.
(I couldn't find a clear duplicate of this with the questions my use case generated. Now that I know the answer I can find questions that discuss it, but I hope this post helps guide someone that way.)
The DbContext type has a Database property that gives you sort of backdoor access to things EF manages. It has a convenience method EnsureCreated (also an async implementation) that will ensure the table exists when the DbContext is configured.
Credit to this EF tutorial for explaining this and a handful of other exotic initializaton scenarios.o
Based on some other things I've read it seems this makes adding migrations later impossible, and perhaps Database.Migrate() does the same thing more safely. So have an idea whether you want migrations at all before you do this (but if you plan on using migrations, it seems silly to be doing manual initializations?)

auto update code first from database model, is it possible?

I have code first classes which are generated from my existing db using ADO.NET Entity Data Model. Now I've added some new tables to the database.
I want to know if its possible to create associated code classes from the new db tables without (re)creating the model again from scratch?
Yes it is, if you use Reverse Engineer Code First to do so. It will create POCOs exactly like Code-First, but it will do so based completely on the current database. No .edmx file and no T4 template. Just Code-First.
You should know, by the way, that this (along with regular Code-First) are going to be the only ones allowed in EF7. They are getting rid of many things to try to slim it down, and both Model- and Database-First got the ax (at least, for now). This blog post from Microsoft's ADO.NET blog explains that, along with some other features.

Quick create Model Class from existing SQL Table

I am using EF Code First in a project with an existing Database, all works well and so far I have been creating the class for each table manually (as they have been very small) but getting to the larger tables I can only assume there must be a better way to 'import' or 'convert' the tables fields into a class somehow. Had a search around and can't really find what I'm looking for.
To clarify I want to keep it Code First.
EF Power Tools contains reverse engineer feature for this purpose.

EF CodeFirst - Create index after database create

I'm migrating my project from database-first to code-first.
Entity Framework does some nice work creating my new database (that should mimic the old one).
I'm using a combination of data annotations and the fluent API to describe my tables.
My database has a few indexes and I would like Entity Framework to create them as well. It seems the old way to do this is to define your own Initializer and use custom T-SQL.
But now that we have EF Migrations it should be easier to do so.
I can't seem to figure out how to combine CreateDatabaseIfNotExists<> with an automatic migration to create the indexes. I've tried to use the MigrateDatabaseToLatestVersion<,> but it doesn't seem to perform the migration after the Database has been created.
What is the proper way to create indexes and constraints on database creation now that we have Entity Framework 4.3?
Don't use CreateDatabaseIfNotExists if you want to use migrations. Use MigrateDatabaseToLatestVersion from the beginning - it will create your database as well. Put your index creation code (calls to CreateIndex) into Up method of your migration class.
If you already have existing database and you want to use migrations you must first create initial migration.

Entity Framework: Modeling against an existing database scheme

I've been scratching my head over this for over a week now and haven't gotten anywhere :( We have an existing legacy DB that I'm trying to model my entities against. The tables are extremely bloated and we do not have enough bandwidth to create new, optimized tables. So I'm having to work with what we already have. However, I do not want to use all the redundant columns that are exposed by the DB. My initial plan was to use Views in my Model but that is looking to be equally hairy with very little documentation around.
Now, what would be the best way to go about creating a Model with just a select few columns? All I need is a bunch of read-only entities; so if there is a way to ignore non-nullable columns from the schema, I'd be all set. I was planning on making use of POCOs else I'd have to create my own mappings I reckon.
UPDATE: By POCOs, I mean I'd like to use the ADO.NET POCO Entity Generator.
What about creating views in the DB, and only importing the views into the model?
Well, if you need only a bunch of entities and if they won't change a lot during time, than I would just pick the tables you need, generate the entities with the normal wizard and all collumns, and than delete all not needed collumns manually in the model designer.
add the table to your EF, and just delete the properties you don't want. it just won't map those DB fields.