شروع به کار با EF Core 1.0 - قسمت 2 - به روز رسانی ساختار بانک اطلاعاتی
نویسنده: وحید نصیری
تاریخ: ۱۳۹۵/۰۵/۲۶ ۱۳:۵۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
D:\Prog\1395\Core1RtmEmptyTest\src\Core1RtmEmptyTest>dotnet ef
_/\__
---==/ \\
___ ___ |. \|\
| __|| __| | ) \\\
| _| | _| \_/ | //|\\
|___||_| / \\\/\\
Entity Framework .NET Core CLI Commands 1.0.0-preview2-21431
Usage: dotnet ef [options] [command]
Options:
-h|--help Show help information
-v|--verbose Enable verbose output
--version Show version information
--assembly <ASSEMBLY> The assembly file to load.
--startup-assembly <ASSEMBLY> The assembly file containing the startup class.
--data-dir <DIR> The folder used as the data directory (defaults to current working directory).
--project-dir <DIR> The folder used as the project directory (defaults to current working directory).
--content-root-path <DIR> The folder used as the content root path for the application (defaults to application base directory).
--root-namespace <NAMESPACE> The root namespace of the target project (defaults to the project assembly name).
Commands:
database Commands to manage your database
dbcontext Commands to manage your DbContext types
migrations Commands to manage your migrations
Use "dotnet ef [command] --help" for more information about a command. D:\Prog\1395\Core1RtmEmptyTest\src\Core1RtmEmptyTest>dotnet ef migrations add InitialDatabase
D:\Prog\1395\Core1RtmEmptyTest\src\Core1RtmEmptyTest>dotnet ef database update Applying migration '13950526050417_InitialDatabase'. Done.
using System.Collections.Generic;
using System.Linq;
using Core1RtmEmptyTest.Entities;
using Microsoft.Extensions.DependencyInjection;
namespace Core1RtmEmptyTest.Migrations
{
public static class ApplicationDbContextSeedData
{
public static void SeedData(this IServiceScopeFactory scopeFactory)
{
using (var serviceScope = scopeFactory.CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
if (!context.Persons.Any())
{
var persons = new List<Person>
{
new Person
{
FirstName = "Admin",
LastName = "User"
}
};
context.AddRange(persons);
context.SaveChanges();
}
}
}
}
} public void Configure(IServiceScopeFactory scopeFactory)
{
scopeFactory.SeedData(); public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(ServiceLifetime.Scoped);
namespace Core1RtmEmptyTest.Entities
{
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
} An unhandled exception occurred while processing the request. SqlException: Invalid column name 'Age'.
D:\Prog\1395\Core1RtmEmptyTest\src\Core1RtmEmptyTest>dotnet ef migrations add v2 D:\Prog\1395\Core1RtmEmptyTest\src\Core1RtmEmptyTest>dotnet ef database update
D:\Prog\1395\Core1RtmEmptyTest\src\Core1RtmEmptyTest>dotnet ef migrations script -o v2.sql
public Configuration()
{
AutomaticMigrationsEnabled = true;
} using Core1RtmEmptyTest.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Core1RtmEmptyTest.Migrations
{
public static class DbInitialization
{
public static void Initialize(this IServiceScopeFactory scopeFactory)
{
using (var serviceScope = scopeFactory.CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
// Applies any pending migrations for the context to the database.
// Will create the database if it does not already exist.
context.Database.Migrate();
}
}
}
} public class SampleDBContext : DbContext
{
private static bool _created = false;
public SampleDBContext()
{
if (!_created) // DropCreateDatabaseAlways
{
_created = true;
Database.EnsureDeleted();
Database.EnsureCreated();
}
}
try
{
using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
db.Database.Migrate();
}
}
catch (Exception ex)
{
//log error
} protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer(
connectionString,
x => x.MigrationsHistoryTable("__MyMigrationsHistory", "mySchema")); protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.HasData(new Blog { BlogId = 1, Url = "http://sample.com" });
} modelBuilder.Entity<Post>().HasData(
new {BlogId = 1, PostId = 1, Title = "First post", Content = "Test 1"},
new {BlogId = 1, PostId = 2, Title = "Second post", Content = "Test 2"}); dotnet tool install --global dotnet-ef --version <exact-version>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design"
Version="3.0.0-preview7.19362.6"
PrivateAssets="all" /> dotnet tool install --global dotnet-ef --version 3.0.0-*
dotnet tool update --global dotnet-ef --version 3.0.0-*
Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
dotnet ef migrations add InitDb -s ../ProjectName.Web
public void ConfigureServices(IServiceCollection services)
{
var connectionString = Configuration["ConnectionStrings:ApplicationDbContextConnection"];
services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(connectionString);
});
// ...
}