Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Database Class

 

An instance of this class is obtained from an DbContext object and can be used to manage the actual database backing a DbContext or connection. This includes creating, deleting, and checking for the existence of a database. Note that deletion and checking for existence of a database can be performed using just a connection (i.e. without a full context) by using the static methods of this class.

Namespace:   System.Data.Entity
Assembly:  EntityFramework (in EntityFramework.dll)

System.Object
  System.Data.Entity.Database

[SuppressMessageAttribute("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable", 
	Justification = "The DbContextTransaction and EntityTransaction should never be disposed by this class")]
public class Database

NameDescription
System_CAPS_pubpropertyCommandTimeout

Gets or sets the timeout value, in seconds, for all context operations. The default value is null, where null indicates that the default value of the underlying provider will be used.

System_CAPS_pubpropertyConnection

Returns the connection being used by this context. This may cause the connection to be created if it does not already exist.

System_CAPS_pubpropertyCurrentTransaction

Gets the transaction the underlying store connection is enlisted in. May be null.

System_CAPS_pubpropertySystem_CAPS_staticDefaultConnectionFactory

Obsolete.The connection factory to use when creating a DbConnection from just a database name or a connection string.

System_CAPS_pubpropertyLog

Set this property to log the SQL generated by the DbContext to the given delegate. For example, to log to the console, set this property to Write.

NameDescription
System_CAPS_pubmethodBeginTransaction()

Begins a transaction on the underlying store connection

System_CAPS_pubmethodBeginTransaction(IsolationLevel)

Begins a transaction on the underlying store connection using the specified isolation level

System_CAPS_pubmethodCompatibleWithModel(Boolean)

Checks whether or not the database is compatible with the the current Code First model.

System_CAPS_pubmethodCreate()

Creates a new database on the database server for the model defined in the backing context. Note that calling this method before the database initialization strategy has run will disable executing that strategy.

System_CAPS_pubmethodCreateIfNotExists()

Creates a new database on the database server for the model defined in the backing context, but only if a database with the same name does not already exist on the server.

System_CAPS_pubmethodDelete()

Deletes the database on the database server if it exists, otherwise does nothing. Calling this method from outside of an initializer will mark the database as having not been initialized. This means that if an attempt is made to use the database again after it has been deleted, then any initializer set will run again and, usually, will try to create the database again automatically.

System_CAPS_pubmethodSystem_CAPS_staticDelete(DbConnection)

Deletes the database on the database server if it exists, otherwise does nothing.

System_CAPS_pubmethodSystem_CAPS_staticDelete(String)

Deletes the database on the database server if it exists, otherwise does nothing. The connection to the database is created using the given database name or connection string in the same way as is described in the documentation for the DbContext class.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified database is equal to the current database.(Overrides Object.Equals(Object).)

System_CAPS_pubmethodExecuteSqlCommand(String, Object[])

Executes the given DDL/DML command against the database. As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.Database.ExecuteSqlCommand("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.Database.ExecuteSqlCommand("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

System_CAPS_pubmethodExecuteSqlCommand(TransactionalBehavior, String, Object[])

Executes the given DDL/DML command against the database. As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.Database.ExecuteSqlCommand("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.Database.ExecuteSqlCommand("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

System_CAPS_pubmethodExecuteSqlCommandAsync(String, CancellationToken, Object[])

Asynchronously executes the given DDL/DML command against the database. As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.Database.ExecuteSqlCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.Database.ExecuteSqlCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

System_CAPS_pubmethodExecuteSqlCommandAsync(String, Object[])

Asynchronously executes the given DDL/DML command against the database. As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.Database.ExecuteSqlCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.Database.ExecuteSqlCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

System_CAPS_pubmethodExecuteSqlCommandAsync(TransactionalBehavior, String, CancellationToken, Object[])

Asynchronously executes the given DDL/DML command against the database. As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.Database.ExecuteSqlCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.Database.ExecuteSqlCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

System_CAPS_pubmethodExecuteSqlCommandAsync(TransactionalBehavior, String, Object[])

Asynchronously executes the given DDL/DML command against the database. As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.Database.ExecuteSqlCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.Database.ExecuteSqlCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

System_CAPS_pubmethodExists()

Checks whether or not the database exists on the server.

System_CAPS_pubmethodSystem_CAPS_staticExists(DbConnection)

Checks whether or not the database exists on the server.

System_CAPS_pubmethodSystem_CAPS_staticExists(String)

Checks whether or not the database exists on the server. The connection to the database is created using the given database name or connection string in the same way as is described in the documentation for the DbContext class.

System_CAPS_protmethodFinalize()

(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Returns the hash function for this database.(Overrides Object.GetHashCode().)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.

System_CAPS_pubmethodInitialize(Boolean)

Runs the the registered IDatabaseInitializer<TContext> on this context. If "force" is set to true, then the initializer is run regardless of whether or not it has been run before. This can be useful if a database is deleted while an app is running and needs to be reinitialized. If "force" is set to false, then the initializer is only run if it has not already been run for this context, model, and connection in this app domain. This method is typically used when it is necessary to ensure that the database has been created and seeded before starting some operation where doing so lazily will cause issues, such as when the operation is part of a transaction.

System_CAPS_protmethodMemberwiseClone()

(Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticSetInitializer<TContext>(IDatabaseInitializer<TContext>)

Sets the database initializer to use for the given context type. The database initializer is called when a the given DbContext type is used to access a database for the first time. The default strategy for Code First contexts is an instance of CreateDatabaseIfNotExists<TContext>.

System_CAPS_pubmethodSqlQuery(Type, String, Object[])

Creates a raw SQL query that will return elements of the given type. The type can be any type that has properties that match the names of the columns returned from the query, or can be a simple primitive type. The type does not have to be an entity type. The results of this query are never tracked by the context even if the type of object returned is an entity type. Use the SqlQuery method to return entities that are tracked by the context. As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.Database.SqlQuery(typeof(Post), "SELECT * FROM dbo.Posts WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.Database.SqlQuery(typeof(Post), "SELECT * FROM dbo.Posts WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

System_CAPS_pubmethodSqlQuery<TElement>(String, Object[])

Creates a raw SQL query that will return elements of the given generic type. The type can be any type that has properties that match the names of the columns returned from the query, or can be a simple primitive type. The type does not have to be an entity type. The results of this query are never tracked by the context even if the type of object returned is an entity type. Use the SqlQuery method to return entities that are tracked by the context. As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.Database.SqlQuery&amp;lt;Post&amp;gt;("SELECT * FROM dbo.Posts WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string. context.Database.SqlQuery&amp;lt;Post&amp;gt;("SELECT * FROM dbo.Posts WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));

System_CAPS_pubmethodToString()

Returns a string representation of the database.(Overrides Object.ToString().)

System_CAPS_pubmethodUseTransaction(DbTransaction)

Enables the user to pass in a database transaction created outside of the Database object if you want the Entity Framework to execute commands within that external transaction. Alternatively, pass in null to clear the framework's knowledge of that transaction.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2015 Microsoft