When using Entity Framework 6 Code-First approach, can I create index directly from database? - entity-framework-6

I have a recommendation from Azure to create the following index:
CREATE NONCLUSTERED INDEX [nci_wi_Transactions_71EA7D20388C3F47A9B9EBF2A1C7BA0C] ON [dbo].[Transactions] ([Client_Id]) INCLUDE ([Date], [Description], [Size], [Type]) WITH (ONLINE = ON)
Is it possible to execute this SQL directly from MSSQL without affecting my applications work?
Or do I need to apply some migrations from code-first to keep database context with the correct state anyway?

Actually, #Steve in comment answered and I checked in runtime and
Yes, it is safe to create an index directly from a database, which will not affect applications, used not changed DbContext.

Related

How to update a database schema in Spring4D 1.2

I'm experimenting with Spring4D 1.2's simple ORM (Marshmallow). I could get it to work pretty well but I can't find out how to have it update an existing database with a new schema.
For instance, in the "GettingStarted" project, I added one property to the data model:
[Entity]
[Table('Products')]
TProduct = class
{...}
[ColumnAttribute('PRODUCTINFO',50)]
property Info: string read fInfo write fInfo;
No matter what, I can't get the framework to update the schema if the database already exists.
What am I missing ? Must I do that manually outside the framework ?
Schema update is currently not supported nor planned from my side.
Generating the necessary drops and recreate would be reasonably easy but that is only part of the story as you probably want to keep any existing data. From using SQL Server Data Tools I know how deep this rabbit hole can go.

Understanding VS's ability to create database on first run

I'm working with a (.net4 / mvc3 ) solution file downloaded (from a reputable source) where a connection string exists in web.config but I don't see explicit instructions to create the database and there's no included '.mdf'. The first time I build I got a runtime error regarding lack of permissions to CREATE database. So I created a blank db and made sure the string referenced a SQL user that had .dbo/owner rights to the db just created.
But subsequent builds don't seem to execute that same initialize db script - where ever that's stored.
Where is this 'first use' convention for creating databases documented?
thx
That is a feature of Entity Framework Code First. I am not sure what you are looking for exactly, but searching for "EF Code First Initialization Strategy" might help.
For instance read this article: EF Code First DB Initialization Using Web.Config
I assume you are talking about Entity Framework, which allows you to create the database from an instance of an ObjectContext object, which is used in any of the three approaches in EF (database-, model- and code-first).
Look for a line that actually calls ObjectContext.CreateDatabase(). If one of the supported ADO.NET provides is used (SQL Server or SQL Server CE 4.0) this will generate the required SQL Statements. Assuming the classic Northwind example, you might find something like that:
NorthwindContext context = new NorthwindContext();
if (!context.DatabaseExists())
{
context.CreateDatabase();
}
If this is in fact a code-first application, "lalibi" is right about the initialization strategy which by default doesn't require you to explicitly create the database. (But my guess is, that it actually uses a statement internally very similar to mine).

EF 4.1 code first with Existing Database.Mapping Fks,table etc.Clarification needed

I am learning code first and I have project to be used with an existing database.
I am a bit confused of what I meant to be doing.I will explain:
Do I need to create an entityconfiguration for each table in my existing database?
Within this EntityConfiguration for each table do I need to create foreign key relationships?
Do I need to do a ToTable for each table in my existing database?
Is there any free tool "codeplex" that pointing to an existing db will generate this codeFirst stuff?
I have seem few blogs about "EF Code first with existing db" but I am not sure or was not clear to me If Need to create this stuff or I will be getting strange errors like "MyColumn_MyColum" basically as if codeFirst is trying to build some FKs or something.
can somebody clarify?
thanks a lot. I know there are few questions but if you can answer 01 or 2 that would be fine.
thanks again
If you want the code to be generated for you use database-first approach with DbContext API. Simply create EDMX file from your database and let DbContext Generator template generate all entities and context for you.
DbContext Fluent API is mainly targeted to the code-first development approach where EF will created database for you from the code you provided. It can be used with existing database but it requires much more skills and understanding of mapping wich EF can provide to you.
Generally:
You don't need to provide EntityConfiguration for each table if you follow some naming conventions (entity name is singular form of table name, all properties have the same name, primary key in table and entity is named as Id or EntityNameId, etc.).
You don't need to define relationships manually if you follow conventions with exposing navigation properties and possibly also foreign key properties. The issue can be naming of many-to-many keys and junction tables.
ToTable is needed only if your entity does not follow naming convention or if you map some advance inheritance or splitting.
EF uses a lot of default conventions which drive how the names should be defined. Conventions can be removed.
You will not do anything wrong if you define EntityConfiguration for each table - it will at least allow you learning what is needed and your mapping will be explicit / self documented.

EF CTP4 - create tables, but not drop DB

I have a single hosted SQL Server DB and I don't have permissions to drop it. How do I make EF create tables from my domain classes? RecreateDatabaseIfModelChanges and AlwaysRecreateDatabase try to drop DB, CreateDatabaseOnlyIfNotExists doesn't create the tables.
Thx
CreateDatabaseOnlyIfNotExists is the default strategy. It means you don't need to even set it through the Database.SetInitializer. EF Code First will check the database and if couldn't find one with the same name as your context's fully qualified name, it will create one for you.

How to update a single field using EF4

I want to update a single field in my table for a particular row. I am using Entity Framework 4 and Visual Studio 2010.
Options I can think of are:
Using a Stored Procedure
Direct connection to the database and using
sql statement
I am not aware of any more efficient method to perform this task.
[EDIT]
I would like to do the update in the same operation as the Get for that row, so that it is done in one DB call.
No need to complicate things. Just change the one property and SaveChanges. Unless you're doing something odd, that should only change the one column. Look at the SQL to verify.

Resources