From 2eef72e43fc11fc9c46e38e007a5ef3c5011fb61 Mon Sep 17 00:00:00 2001 From: Youssef Victor <31348972+Youssef1313@users.noreply.github.com> Date: Wed, 20 Nov 2019 01:00:36 +0200 Subject: [PATCH] Use dotnetcli (#1922) * Update index.md * Update index.md * Update index.md * Update index.md * Update index.md * dotnetcli * fix indentation * More fixes --- entity-framework/core/get-started/index.md | 8 +- .../core/get-started/install/index.md | 4 +- .../core/managing-schemas/migrations/index.md | 14 +- .../managing-schemas/migrations/projects.md | 2 +- .../managing-schemas/migrations/providers.md | 2 +- .../core/managing-schemas/scaffolding.md | 8 +- .../core/miscellaneous/1x-2x-upgrade.md | 2 +- .../core/miscellaneous/cli/dotnet.md | 20 +- .../core/providers/cosmos/index.md | 8 +- .../core/providers/in-memory/index.md | 2 +- entity-framework/core/providers/index.md | 2 +- .../core/providers/sql-server/index.md | 2 +- .../core/providers/sqlite/index.md | 2 +- .../ef-core-3.0/breaking-changes.md | 82 +++---- .../core/what-is-new/ef-core-3.0/index.md | 2 +- .../ef6/modeling/code-first/fluent/vb.md | 216 +++++++++--------- 16 files changed, 189 insertions(+), 187 deletions(-) diff --git a/entity-framework/core/get-started/index.md b/entity-framework/core/get-started/index.md index 0334ba5416..39f60324e1 100644 --- a/entity-framework/core/get-started/index.md +++ b/entity-framework/core/get-started/index.md @@ -33,7 +33,7 @@ Install the following software: ## [.NET Core CLI](#tab/netcore-cli) -``` Console +```dotnetcli dotnet new console -o EFGetStarted cd EFGetStarted ``` @@ -53,7 +53,7 @@ To install EF Core, you install the package for the EF Core database provider(s) ## [.NET Core CLI](#tab/netcore-cli) -``` Console +```dotnetcli dotnet add package Microsoft.EntityFrameworkCore.Sqlite ``` @@ -100,7 +100,7 @@ The following steps use [migrations](xref:core/managing-schemas/migrations/index * Run the following commands: - ``` Console + ```dotnetcli dotnet tool install --global dotnet-ef dotnet add package Microsoft.EntityFrameworkCore.Design dotnet ef migrations add InitialCreate @@ -133,7 +133,7 @@ The following steps use [migrations](xref:core/managing-schemas/migrations/index ## [.NET Core CLI](#tab/netcore-cli) -``` Console +```dotnetcli dotnet run ``` diff --git a/entity-framework/core/get-started/install/index.md b/entity-framework/core/get-started/install/index.md index 76a94dfa6b..804fa05e2a 100644 --- a/entity-framework/core/get-started/install/index.md +++ b/entity-framework/core/get-started/install/index.md @@ -31,7 +31,7 @@ To install or update NuGet packages, you can use the .NET Core command-line inte * Use the following .NET Core CLI command from the operating system's command line to install or update the EF Core SQL Server provider: - ``` Console + ```dotnetcli dotnet add package Microsoft.EntityFrameworkCore.SqlServer ``` @@ -89,7 +89,7 @@ Although you can also use the `dotnet ef` commands from the Package Manager Cons The `dotnet ef` commands are included in current versions of the .NET Core SDK, but to enable the commands on a specific project, you have to install the `Microsoft.EntityFrameworkCore.Design` package: -``` Console +```dotnetcli dotnet add package Microsoft.EntityFrameworkCore.Design ``` diff --git a/entity-framework/core/managing-schemas/migrations/index.md b/entity-framework/core/managing-schemas/migrations/index.md index 95b79816a2..cf00685c5b 100644 --- a/entity-framework/core/managing-schemas/migrations/index.md +++ b/entity-framework/core/managing-schemas/migrations/index.md @@ -35,7 +35,7 @@ After you've [defined your initial model](xref:core/modeling/index), it's time t ## [.NET Core CLI](#tab/dotnet-core-cli) -``` Console +```dotnetcli dotnet ef migrations add InitialCreate ``` @@ -64,7 +64,7 @@ Next, apply the migration to the database to create the schema. ## [.NET Core CLI](#tab/dotnet-core-cli) -``` Console +```dotnetcli dotnet ef database update ``` @@ -82,7 +82,7 @@ After making changes to your EF Core model, the database schema might be out of ## [.NET Core CLI](#tab/dotnet-core-cli) -``` Console +```dotnetcli dotnet ef migrations add AddProductReviews ``` @@ -143,7 +143,7 @@ Apply the migration to the database using the appropriate command. ## [.NET Core CLI](#tab/dotnet-core-cli) -``` Console +```dotnetcli dotnet ef database update ``` @@ -171,7 +171,7 @@ Sometimes you add a migration and realize you need to make additional changes to ## [.NET Core CLI](#tab/dotnet-core-cli) -``` Console +```dotnetcli dotnet ef migrations remove ``` @@ -191,7 +191,7 @@ If you already applied a migration (or several migrations) to the database but n ## [.NET Core CLI](#tab/dotnet-core-cli) -``` Console +```dotnetcli dotnet ef database update LastGoodMigration ``` @@ -209,7 +209,7 @@ When debugging your migrations or deploying them to a production database, it's ## [.NET Core CLI](#tab/dotnet-core-cli) -``` Console +```dotnetcli dotnet ef migrations script ``` diff --git a/entity-framework/core/managing-schemas/migrations/projects.md b/entity-framework/core/managing-schemas/migrations/projects.md index c653603692..6e07461d73 100644 --- a/entity-framework/core/managing-schemas/migrations/projects.md +++ b/entity-framework/core/managing-schemas/migrations/projects.md @@ -42,7 +42,7 @@ If you did everything correctly, you should be able to add new migrations to the ## [.NET Core CLI](#tab/dotnet-core-cli) -``` Console +```dotnetcli dotnet ef migrations add NewMigration --project MyApp.Migrations ``` diff --git a/entity-framework/core/managing-schemas/migrations/providers.md b/entity-framework/core/managing-schemas/migrations/providers.md index 974f6847ed..6015cf40f7 100644 --- a/entity-framework/core/managing-schemas/migrations/providers.md +++ b/entity-framework/core/managing-schemas/migrations/providers.md @@ -34,7 +34,7 @@ When adding new migration, specify the context types. ## [.NET Core CLI](#tab/dotnet-core-cli) -``` Console +```dotnetcli dotnet ef migrations add InitialCreate --context MyDbContext --output-dir Migrations/SqlServerMigrations dotnet ef migrations add InitialCreate --context MySqliteDbContext --output-dir Migrations/SqliteMigrations ``` diff --git a/entity-framework/core/managing-schemas/scaffolding.md b/entity-framework/core/managing-schemas/scaffolding.md index c3cdaae403..d7bbf011ea 100644 --- a/entity-framework/core/managing-schemas/scaffolding.md +++ b/entity-framework/core/managing-schemas/scaffolding.md @@ -26,7 +26,7 @@ How you quote and escape the connection string depends on which shell you are us Scaffold-DbContext 'Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Chinook' Microsoft.EntityFrameworkCore.SqlServer ``` -``` Console +```dotnetcli dotnet ef dbcontext scaffold "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Chinook" Microsoft.EntityFrameworkCore.SqlServer ``` @@ -36,7 +36,7 @@ If you have an ASP.NET Core project, you can use the `Name=` This works well with the [Secret Manager tool](https://docs.microsoft.com/aspnet/core/security/app-secrets#secret-manager) to keep your database password separate from your codebase. -``` Console +```dotnetcli dotnet user-secrets set ConnectionStrings.Chinook "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Chinook" dotnet ef dbcontext scaffold Name=Chinook Microsoft.EntityFrameworkCore.SqlServer ``` @@ -61,7 +61,7 @@ Scaffold-DbContext ... -Tables Artist, Album To include multiple tables in the CLI, specify the option multiple times. -``` Console +```dotnetcli dotnet ef dbcontext scaffold ... --table Artist --table Album ``` @@ -103,7 +103,7 @@ You can also use `-ContextDir` (PMC) and `--context-dir` (CLI) to scaffold the D Scaffold-DbContext ... -ContextDir Data -OutputDir Models ``` -``` Console +```dotnetcli dotnet ef dbcontext scaffold ... --context-dir Data --output-dir Models ``` diff --git a/entity-framework/core/miscellaneous/1x-2x-upgrade.md b/entity-framework/core/miscellaneous/1x-2x-upgrade.md index a0e01698e3..327ef8a703 100644 --- a/entity-framework/core/miscellaneous/1x-2x-upgrade.md +++ b/entity-framework/core/miscellaneous/1x-2x-upgrade.md @@ -115,7 +115,7 @@ var tableName = context.Model.FindEntityType(typeof(User)).Relational().TableNam Instead of using methods like `ForSqlServerToTable`, extension methods are now available to write conditional code based on the current provider in use. For example: -```C# +```csharp modelBuilder.Entity().ToTable( Database.IsSqlServer() ? "SqlServerName" : "OtherName"); ``` diff --git a/entity-framework/core/miscellaneous/cli/dotnet.md b/entity-framework/core/miscellaneous/cli/dotnet.md index e0b48da150..bfb6810bc8 100644 --- a/entity-framework/core/miscellaneous/cli/dotnet.md +++ b/entity-framework/core/miscellaneous/cli/dotnet.md @@ -28,7 +28,7 @@ The installation procedure depends on project type and version: * `dotnet ef` must be installed as a global or local tool. Most developers will install `dotnet ef` as a global tool with the following command: - ``` console + ```dotnetcli dotnet tool install --global dotnet-ef ``` @@ -38,7 +38,7 @@ The installation procedure depends on project type and version: * Install the latest `Microsoft.EntityFrameworkCore.Design` package. - ``` Console + ```dotnetcli dotnet add package Microsoft.EntityFrameworkCore.Design ``` @@ -56,7 +56,7 @@ The `dotnet ef` commands are included in the .NET Core SDK, but to enable the co * Install the latest stable `Microsoft.EntityFrameworkCore.Design` package. - ``` Console + ```dotnetcli dotnet add package Microsoft.EntityFrameworkCore.Design ``` @@ -70,7 +70,7 @@ The `dotnet ef` commands are included in the .NET Core SDK, but to enable the co * Install the latest 1.x version of the `Microsoft.EntityFrameworkCore.Design` package, for example: - ```console + ```dotnetcli dotnet add package Microsoft.EntityFrameworkCore.Design -v 1.1.6 ``` @@ -100,7 +100,7 @@ The `dotnet ef` commands are included in the .NET Core SDK, but to enable the co Run the following commands to verify that EF Core CLI tools are correctly installed: - ``` Console + ```dotnetcli dotnet restore dotnet ef ``` @@ -189,7 +189,7 @@ Arguments: The following examples update the database to a specified migration. The first uses the migration name and the second uses the migration ID: -```console +```dotnetcli dotnet ef database update InitialCreate dotnet ef database update 20180904195021_InitialCreate ``` @@ -228,13 +228,13 @@ Options: The following example scaffolds all schemas and tables and puts the new files in the *Models* folder. -```console +```dotnetcli dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models ``` The following example scaffolds only selected tables and creates the context in a separate folder with a specified name: -```console +```dotnetcli dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models -t Blog -t Post --context-dir Context -c BlogContext ``` @@ -288,13 +288,13 @@ Options: The following example creates a script for the InitialCreate migration: -```console +```dotnetcli dotnet ef migrations script 0 InitialCreate ``` The following example creates a script for all migrations after the InitialCreate migration. -```console +```dotnetcli dotnet ef migrations script 20180904195021_InitialCreate ``` diff --git a/entity-framework/core/providers/cosmos/index.md b/entity-framework/core/providers/cosmos/index.md index 3a33db812d..7ee5efc0c6 100644 --- a/entity-framework/core/providers/cosmos/index.md +++ b/entity-framework/core/providers/cosmos/index.md @@ -8,14 +8,14 @@ uid: core/providers/cosmos/index --- # EF Core Azure Cosmos DB Provider ->[!NOTE] +> [!NOTE] > This provider is new in EF Core 3.0. This database provider allows Entity Framework Core to be used with Azure Cosmos DB. The provider is maintained as part of the [Entity Framework Core Project](https://github.com/aspnet/EntityFrameworkCore). It is strongly recommended to familiarize yourself with the [Azure Cosmos DB documentation](/azure/cosmos-db/introduction) before reading this section. ->[!NOTE] +> [!NOTE] > This provider only works with the SQL API of Azure Cosmos DB. ## Install @@ -24,7 +24,7 @@ Install the [Microsoft.EntityFrameworkCore.Cosmos NuGet package](https://www.nug ## [.NET Core CLI](#tab/dotnet-core-cli) -``` console +```dotnetcli dotnet add package Microsoft.EntityFrameworkCore.Cosmos ``` @@ -83,7 +83,7 @@ By default EF Core will create containers with the partition key set to `"__part [!code-csharp[PartitionKey](../../../../samples/core/Cosmos/ModelBuilding/OrderContext.cs?name=PartitionKey)] ->[!NOTE] +> [!NOTE] >The partition key property can be of any type as long as it is [converted to string](xref:core/modeling/value-conversions). Once configured the partition key property should always have a non-null value. When issuing a query a condition can be added to make it single-partition. diff --git a/entity-framework/core/providers/in-memory/index.md b/entity-framework/core/providers/in-memory/index.md index 1630d68661..8d1d7c99f6 100644 --- a/entity-framework/core/providers/in-memory/index.md +++ b/entity-framework/core/providers/in-memory/index.md @@ -15,7 +15,7 @@ Install the [Microsoft.EntityFrameworkCore.InMemory NuGet package](https://www.n ## [.NET Core CLI](#tab/dotnet-core-cli) -``` console +```dotnetcli dotnet add package Microsoft.EntityFrameworkCore.InMemory ``` diff --git a/entity-framework/core/providers/index.md b/entity-framework/core/providers/index.md index e4c8809344..250e298347 100644 --- a/entity-framework/core/providers/index.md +++ b/entity-framework/core/providers/index.md @@ -47,7 +47,7 @@ Most database providers for EF Core are distributed as NuGet packages, and can b ## [.NET Core CLI](#tab/dotnet-core-cli) -``` console +```dotnetcli dotnet add package provider_package_name ``` diff --git a/entity-framework/core/providers/sql-server/index.md b/entity-framework/core/providers/sql-server/index.md index 7b0515fa14..394dbc823e 100644 --- a/entity-framework/core/providers/sql-server/index.md +++ b/entity-framework/core/providers/sql-server/index.md @@ -16,7 +16,7 @@ Install the [Microsoft.EntityFrameworkCore.SqlServer NuGet package](https://www. ## [.NET Core CLI](#tab/dotnet-core-cli) -``` console +```dotnetcli dotnet add package Microsoft.EntityFrameworkCore.SqlServer ``` diff --git a/entity-framework/core/providers/sqlite/index.md b/entity-framework/core/providers/sqlite/index.md index 27cec8c2e2..3b7ac12d43 100644 --- a/entity-framework/core/providers/sqlite/index.md +++ b/entity-framework/core/providers/sqlite/index.md @@ -15,7 +15,7 @@ Install the [Microsoft.EntityFrameworkCore.Sqlite NuGet package](https://www.nug ## [.NET Core CLI](#tab/dotnet-core-cli) -``` console +```dotnetcli dotnet add package Microsoft.EntityFrameworkCore.Sqlite ``` diff --git a/entity-framework/core/what-is-new/ef-core-3.0/breaking-changes.md b/entity-framework/core/what-is-new/ef-core-3.0/breaking-changes.md index ad238a2181..746e169ba9 100644 --- a/entity-framework/core/what-is-new/ef-core-3.0/breaking-changes.md +++ b/entity-framework/core/what-is-new/ef-core-3.0/breaking-changes.md @@ -185,7 +185,7 @@ Before EF Core 3.0, these method names were overloaded to work with either a nor Starting with EF Core 3.0, use `FromSqlRaw`, `ExecuteSqlRaw`, and `ExecuteSqlRawAsync` to create a parameterized query where the parameters are passed separately from the query string. For example: -```C# +```csharp context.Products.FromSqlRaw( "SELECT * FROM Products WHERE Name = {0}", product.Name); @@ -194,7 +194,7 @@ context.Products.FromSqlRaw( Use `FromSqlInterpolated`, `ExecuteSqlInterpolated`, and `ExecuteSqlInterpolatedAsync` to create a parameterized query where the parameters are passed as part of an interpolated query string. For example: -```C# +```csharp context.Products.FromSqlInterpolated( $"SELECT * FROM Products WHERE Name = {product.Name}"); ``` @@ -219,7 +219,7 @@ Switch to use the new method names. Before EF Core 3.0, FromSql method tried to detect if the passed SQL can be composed upon. It did client evaluation when the SQL was non-composable like a stored procedure. The following query worked by running the stored procedure on the server and doing FirstOrDefault on the client side. -```C# +```csharp context.Products.FromSqlRaw("[dbo].[Ten Most Expensive Products]").FirstOrDefault(); ``` @@ -235,7 +235,7 @@ EF Core 3.0 does not support automatic client evaluation, since it was error pro If you are using a stored procedure in FromSqlRaw/FromSqlInterpolated, you know that it cannot be composed upon, so you can add __AsEnumerable/AsAsyncEnumerable__ right after the FromSql method call to avoid any composition on server side. -```C# +```csharp context.Products.FromSqlRaw("[dbo].[Ten Most Expensive Products]").AsEnumerable().FirstOrDefault(); ``` @@ -270,7 +270,7 @@ Specifying `FromSql` anywhere other than on a `DbSet` had no added meaning or ad Before EF Core 3.0, the same entity instance would be used for every occurrence of an entity with a given type and ID. This matches the behavior of tracking queries. For example, this query: -```C# +```csharp var results = context.Products.Include(e => e.Category).AsNoTracking().ToList(); ``` would return the same `Category` instance for each `Product` that is associated with the given category. @@ -294,7 +294,7 @@ Use a tracking query if identity resolution is required. [Tracking Issue #14523](https://github.com/aspnet/EntityFrameworkCore/issues/14523) We reverted this change because new configuration in EF Core 3.0 allows the log level for any event to be specified by the application. For example, to switch logging of SQL to `Debug`, explicitly configure the level in `OnConfiguring` or `AddDbContext`: -```C# +```csharp protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder .UseSqlServer(connectionString) @@ -355,7 +355,7 @@ This change can break an application if an entity type is configured to use gene The fix is to explicitly configure the key properties to not use generated values. For example, with the fluent API: -```C# +```csharp modelBuilder .Entity() .Property(e => e.Id) @@ -364,7 +364,7 @@ modelBuilder Or with data annotations: -```C# +```csharp [DatabaseGenerated(DatabaseGeneratedOption.None)] public string Id { get; set; } ``` @@ -391,7 +391,7 @@ This change was made to improve the experience for data binding and auditing sce The previous behavior can be restored through settings on `context.ChangedTracker`. For example: -```C# +```csharp context.ChangeTracker.CascadeDeleteTiming = CascadeTiming.OnSaveChanges; context.ChangeTracker.DeleteOrphansTiming = CascadeTiming.OnSaveChanges; ``` @@ -484,7 +484,7 @@ Before EF Core 3.0, configuration of the owned relationship was performed direct Starting with EF Core 3.0, there is now fluent API to configure a navigation property to the owner using `WithOwner()`. For example: -```C# +```csharp modelBuilder.Entity.OwnsOne(e => e.Details).WithOwner(e => e.Order); ``` @@ -492,7 +492,7 @@ The configuration related to the relationship between owner and owned should now While the configuration for the owned type itself would still be chained after `OwnsOne()/OwnsMany()`. For example: -```C# +```csharp modelBuilder.Entity.OwnsOne(e => e.Details, eb => { eb.WithOwner() @@ -534,7 +534,7 @@ Change configuration of owned type relationships to use the new API surface as s **Old behavior** Consider the following model: -```C# +```csharp public class Order { public int Id { get; set; } @@ -569,7 +569,7 @@ If your model has a table sharing dependent with all optional columns, but the n **Old behavior** Consider the following model: -```C# +```csharp public class Order { public int Id { get; set; } @@ -604,7 +604,7 @@ This change was made to avoid a stale concurrency token value when only one of t **Mitigations** All entities sharing the table have to include a property that is mapped to the concurrency token column. It's possible the create one in shadow-state: -```C# +```csharp protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() @@ -621,7 +621,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) **Old behavior** Consider the following model: -```C# +```csharp public abstract class EntityBase { public int Id { get; set; } @@ -663,7 +663,7 @@ The old behavoir was unexpected. The property can still be explicitly mapped to separate column on the derived types: -```C# +```csharp protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Ignore(); @@ -684,7 +684,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) **Old behavior** Consider the following model: -```C# +```csharp public class Customer { public int CustomerId { get; set; } @@ -706,7 +706,7 @@ Starting with 3.0, EF Core doesn't try to use properties for foreign keys by con Principal type name concatenated with principal property name, and navigation name concatenated with principal property name patterns are still matched. For example: -```C# +```csharp public class Customer { public int Id { get; set; } @@ -720,7 +720,7 @@ public class Order } ``` -```C# +```csharp public class Customer { public int Id { get; set; } @@ -753,7 +753,7 @@ If the property was intended to be the foreign key, and hence part of the primar Before EF Core 3.0, if the context opens the connection inside a `TransactionScope`, the connection remains open while the current `TransactionScope` is active. -```C# +```csharp using (new TransactionScope()) { using (AdventureWorks context = new AdventureWorks()) @@ -762,7 +762,7 @@ using (new TransactionScope()) context.SaveChanges(); // Old behavior: Connection is still open at this point - + var categories = context.ProductCategories().ToList(); } } @@ -780,7 +780,7 @@ This change allows to use multiple contexts in the same `TransactionScope`. The If the connection needs to remain open explicit call to `OpenConnection()` will ensure that EF Core doesn't close it prematurely: -```C# +```csharp using (new TransactionScope()) { using (AdventureWorks context = new AdventureWorks()) @@ -788,7 +788,7 @@ using (new TransactionScope()) context.Database.OpenConnection(); context.ProductCategories.Add(new ProductCategory()); context.SaveChanges(); - + var categories = context.ProductCategories().ToList(); context.Database.CloseConnection(); } @@ -842,7 +842,7 @@ This change was made to prevent EF Core from erroneously triggering business log The pre-3.0 behavior can be restored through configuration of the property access mode on `ModelBuilder`. For example: -```C# +```csharp modelBuilder.UsePropertyAccessMode(PropertyAccessMode.PreferFieldDuringConstruction); ``` @@ -868,7 +868,7 @@ This change was made to avoid silently using one field over another when only on Properties with ambiguous backing fields must have the field to use specified explicitly. For example, using the fluent API: -```C# +```csharp modelBuilder .Entity() .Property(e => e.Id) @@ -880,14 +880,16 @@ modelBuilder **Old behavior** Before EF Core 3.0, a property could be specified by a string value and if no property with that name was found on the .NET type then EF Core would try to match it to a field using convention rules. -```C# + +```csharp private class Blog { private int _id; public string Name { get; set; } } ``` -```C# + +```csharp modelBuilder .Entity() .Property("Id"); @@ -897,7 +899,7 @@ modelBuilder Starting with EF Core 3.0, a field-only property must match the field name exactly. -```C# +```csharp modelBuilder .Entity() .Property("_id"); @@ -912,7 +914,7 @@ This change was made to avoid using the same field for two properties named simi Field-only properties must be named the same as the field they are mapped to. In a future release of EF Core after 3.0, we plan to re-enable explicitly configuring a field name that is different from the property name (see issue [#15307](https://github.com/aspnet/EntityFrameworkCore/issues/15307)): -```C# +```csharp modelBuilder .Entity() .Property("Id") @@ -991,7 +993,7 @@ This change was made because client-generated `string`/`byte[]` values generally The pre-3.0 behavior can be obtained by explicitly specifying that the key properties should use generated values if no other non-null value is set. For example, with the fluent API: -```C# +```csharp modelBuilder .Entity() .Property(e => e.Id) @@ -1000,7 +1002,7 @@ modelBuilder Or with data annotations: -```C# +```csharp [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public string Id { get; set; } ``` @@ -1078,7 +1080,7 @@ The most appropriate cause of action on encountering this error is to understand However, the error can be converted back to a warning (or ignored) via configuration on the `DbContextOptionsBuilder`. For example: -```C# +```csharp protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder @@ -1096,7 +1098,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) Before EF Core 3.0, code calling `HasOne` or `HasMany` with a single string was interpreted in a confusing way. For example: -```C# +```csharp modelBuilder.Entity().HasOne("Entrance").WithOne(); ``` @@ -1119,7 +1121,7 @@ This is not common. The previous behavior can be obtained through explicitly passing `null` for the navigation property name. For example: -```C# +```csharp modelBuilder.Entity().HasOne("Some.Entity.Type.Name", null).WithOne(); ``` @@ -1509,7 +1511,7 @@ Use the new name. (Note that the event ID number has not changed.) Before EF Core 3.0, foreign key constraint names were referred to as simply the "name". For example: -```C# +```csharp var constraintName = myForeignKey.Name; ``` @@ -1517,7 +1519,7 @@ var constraintName = myForeignKey.Name; Starting with EF Core 3.0, foreign key constraint names are now referred to as the "constraint name". For example: -```C# +```csharp var constraintName = myForeignKey.ConstraintName; ``` @@ -1659,7 +1661,7 @@ of API compatibility, this should only be a simple package and namespace change. An entity type with multiple self-referencing uni-directional navigation properties and matching FKs was incorrectly configured as a single relationship. For example: -```C# +```csharp public class User { public Guid Id { get; set; } @@ -1682,7 +1684,7 @@ The resultant model was ambiguous and will likely usually be wrong for this case Use full configuration of the relationship. For example: -```C# +```csharp modelBuilder .Entity() .HasOne(e => e.CreatedBy) @@ -1703,7 +1705,7 @@ modelBuilder A DbFunction configured with schema as an empty string was treated as built-in function without a schema. For example following code will map `DatePart` CLR function to `DATEPART` built-in function on SqlServer. -```C# +```csharp [DbFunction("DATEPART", Schema = "")] public static int? DatePart(string datePartArg, DateTime? date) => throw new Exception(); @@ -1721,7 +1723,7 @@ Previously schema being empty was a way to treat that function is built-in but t Configure DbFunction's translation manually to map it to a built-in function. -```C# +```csharp modelBuilder .HasDbFunction(typeof(MyContext).GetMethod(nameof(MyContext.DatePart))) .HasTranslation(args => SqlFunctionExpression.Create("DatePart", args, typeof(int?), null)); diff --git a/entity-framework/core/what-is-new/ef-core-3.0/index.md b/entity-framework/core/what-is-new/ef-core-3.0/index.md index 2fd10bd6b6..59e93307b4 100644 --- a/entity-framework/core/what-is-new/ef-core-3.0/index.md +++ b/entity-framework/core/what-is-new/ef-core-3.0/index.md @@ -140,7 +140,7 @@ As they are an excellent fit for mapping database views in most scenarios, EF Co For example, using the [dotnet ef command-line tool](xref:core/miscellaneous/cli/dotnet) you can type: -``` console +```dotnetcli dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer ``` diff --git a/entity-framework/ef6/modeling/code-first/fluent/vb.md b/entity-framework/ef6/modeling/code-first/fluent/vb.md index 53587dd2dc..8cdb8b0ac7 100644 --- a/entity-framework/ef6/modeling/code-first/fluent/vb.md +++ b/entity-framework/ef6/modeling/code-first/fluent/vb.md @@ -36,95 +36,95 @@ In this step you will define VB.NET POCO entity types that represent the concept - Replace the contents of the new class with the following code ``` vb - Public Class Department - Public Sub New() - Me.Courses = New List(Of Course)() - End Sub - - ' Primary key - Public Property DepartmentID() As Integer - Public Property Name() As String - Public Property Budget() As Decimal - Public Property StartDate() As Date - Public Property Administrator() As Integer? - Public Overridable Property Courses() As ICollection(Of Course) - End Class - - Public Class Course - Public Sub New() - Me.Instructors = New HashSet(Of Instructor)() - End Sub - - ' Primary key - Public Property CourseID() As Integer - Public Property Title() As String - Public Property Credits() As Integer - - ' Foreign key that does not follow the Code First convention. - ' The fluent API will be used to configure DepartmentID_FK to be the foreign key for this entity. - Public Property DepartmentID_FK() As Integer - - ' Navigation properties - Public Overridable Property Department() As Department - Public Overridable Property Instructors() As ICollection(Of Instructor) - End Class - - Public Class OnlineCourse - Inherits Course - - Public Property URL() As String - End Class - - Partial Public Class OnsiteCourse - Inherits Course - - Public Sub New() - Details = New OnsiteCourseDetails() - End Sub - - Public Property Details() As OnsiteCourseDetails - End Class - - ' Complex type - Public Class OnsiteCourseDetails - Public Property Time() As Date - Public Property Location() As String - Public Property Days() As String - End Class - - Public Class Person - ' Primary key - Public Property PersonID() As Integer - Public Property LastName() As String - Public Property FirstName() As String - End Class - - Public Class Instructor - Inherits Person - - Public Sub New() - Me.Courses = New List(Of Course)() - End Sub - - Public Property HireDate() As Date - - ' Navigation properties - Private privateCourses As ICollection(Of Course) - Public Overridable Property Courses() As ICollection(Of Course) - Public Overridable Property OfficeAssignment() As OfficeAssignment - End Class - - Public Class OfficeAssignment - ' Primary key that does not follow the Code First convention. - ' The HasKey method is used later to configure the primary key for the entity. - Public Property InstructorID() As Integer - - Public Property Location() As String - Public Property Timestamp() As Byte() - - ' Navigation property - Public Overridable Property Instructor() As Instructor - End Class +Public Class Department + Public Sub New() + Me.Courses = New List(Of Course)() + End Sub + + ' Primary key + Public Property DepartmentID() As Integer + Public Property Name() As String + Public Property Budget() As Decimal + Public Property StartDate() As Date + Public Property Administrator() As Integer? + Public Overridable Property Courses() As ICollection(Of Course) +End Class + +Public Class Course + Public Sub New() + Me.Instructors = New HashSet(Of Instructor)() + End Sub + + ' Primary key + Public Property CourseID() As Integer + Public Property Title() As String + Public Property Credits() As Integer + + ' Foreign key that does not follow the Code First convention. + ' The fluent API will be used to configure DepartmentID_FK to be the foreign key for this entity. + Public Property DepartmentID_FK() As Integer + + ' Navigation properties + Public Overridable Property Department() As Department + Public Overridable Property Instructors() As ICollection(Of Instructor) +End Class + +Public Class OnlineCourse + Inherits Course + + Public Property URL() As String +End Class + +Partial Public Class OnsiteCourse + Inherits Course + + Public Sub New() + Details = New OnsiteCourseDetails() + End Sub + + Public Property Details() As OnsiteCourseDetails + End Class + +' Complex type +Public Class OnsiteCourseDetails + Public Property Time() As Date + Public Property Location() As String + Public Property Days() As String +End Class + +Public Class Person + ' Primary key + Public Property PersonID() As Integer + Public Property LastName() As String + Public Property FirstName() As String +End Class + +Public Class Instructor + Inherits Person + + Public Sub New() + Me.Courses = New List(Of Course)() + End Sub + + Public Property HireDate() As Date + + ' Navigation properties + Private privateCourses As ICollection(Of Course) + Public Overridable Property Courses() As ICollection(Of Course) + Public Overridable Property OfficeAssignment() As OfficeAssignment +End Class + +Public Class OfficeAssignment + ' Primary key that does not follow the Code First convention. + ' The HasKey method is used later to configure the primary key for the entity. + Public Property InstructorID() As Integer + + Public Property Location() As String + Public Property Timestamp() As Byte() + + ' Navigation property + Public Overridable Property Instructor() As Instructor +End Class ``` ## Define a Derived Context @@ -143,24 +143,24 @@ Now it’s time to define a derived context, which represents a session with the - Add a new class to the project, enter **SchoolContext** for the class name - Replace the contents of the new class with the following code -``` vb - Imports System.Data.Entity - Imports System.Data.Entity.Infrastructure - Imports System.Data.Entity.ModelConfiguration.Conventions - Imports System.ComponentModel.DataAnnotations - Imports System.ComponentModel.DataAnnotations.Schema - - Public Class SchoolContext - Inherits DbContext - - Public Property OfficeAssignments() As DbSet(Of OfficeAssignment) - Public Property Instructors() As DbSet(Of Instructor) - Public Property Courses() As DbSet(Of Course) - Public Property Departments() As DbSet(Of Department) - - Protected Overrides Sub OnModelCreating(ByVal modelBuilder As DbModelBuilder) - End Sub - End Class +```vb +Imports System.ComponentModel.DataAnnotations +Imports System.ComponentModel.DataAnnotations.Schema +Imports System.Data.Entity +Imports System.Data.Entity.Infrastructure +Imports System.Data.Entity.ModelConfiguration.Conventions + +Public Class SchoolContext + Inherits DbContext + + Public Property OfficeAssignments() As DbSet(Of OfficeAssignment) + Public Property Instructors() As DbSet(Of Instructor) + Public Property Courses() As DbSet(Of Course) + Public Property Departments() As DbSet(Of Department) + + Protected Overrides Sub OnModelCreating(ByVal modelBuilder As DbModelBuilder) + End Sub +End Class ``` ## Configuring with the Fluent API @@ -372,7 +372,7 @@ Module Module1 Sub Main() - Using context As New SchoolContext() + Using context As New SchoolContext() ' Create and save a new Department. Console.Write("Enter a name for a new Department: ")