2017-02-12 21 views
13

Tôi cố gắng để áp dụng Jimmy Bogard của ContosoUniversityCore projectLàm thế nào để thiết lập Migrations EF6 với ASP.NET Lõi

Tôi muốn làm mã di cư đầu tiên, nhưng không chắc chắn làm thế nào để đúng cách thiết lập nó. Tôi đã thêm Migrator.EF6.Tools vào dự án của mình.

Khi tôi chạy Enable-Migrations tôi nhận được lỗi này:

Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not 
marked as serializable." 
At C:\Users\SomeUser\.nuget\packages\entityframework\6.1.3\tools\EntityFramework.psm1:718 char:5 
+  $domain.SetData('project', $project) 
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : SerializationException 

Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not 
marked as serializable." 
At C:\Users\SomeUser\.nuget\packages\entityframework\6.1.3\tools\EntityFramework.psm1:719 char:5 
+  $domain.SetData('contextProject', $contextProject) 
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : SerializationException 

Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not 
marked as serializable." 
At C:\Users\SomeUser\.nuget\packages\entityframework\6.1.3\tools\EntityFramework.psm1:720 char:5 
+  $domain.SetData('startUpProject', $startUpProject) 
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : SerializationException 

System.NullReferenceException: Object reference not set to an instance of an object. 
    at System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetPropertyValue[T](Project project, String propertyName) 
    at System.Data.Entity.Migrations.MigrationsDomainCommand.GetFacade(String configurationTypeName, Boolean useContextWorkingDirectory) 
    at System.Data.Entity.Migrations.EnableMigrationsCommand.FindContextToEnable(String contextTypeName) 
    at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0() 
    at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command) 
Object reference not set to an instance of an object. 

Trả lời

6

Vấn đề thực sự ở đây là có EF khác nhau "mùi vị". Chỉ cần vào tài liệu gốc EF và thấy sự khác biệt:

Dưới đây là công thức của tôi để sử dụng di cư với EF 6 và NET Core:

1st.- Bạn phải thêm những dòng này đến csproj:

<ItemGroup> 
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" /> 
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.1" /> 
</ItemGroup> 

Lưu ý: Phiên bản có thay đổi

2nd.- Tạo một Bối cảnh trong dự án của bạn như thế này:

public class YourContext : DbContext 
{ 
    #region Constructors 

    public YourContext() 
    { 
    } 

    public YourContext(DbContextOptions options) : base(options) 
    { 

    } 

    #region DbSets // YOUR DB SETS GO HERE... 
    #endregion DbSets 

    #region OnConfiguring // THIS HELPED ME A LOT: 
    protected override void OnConfiguring(DbContextOptionsBuilder options) 
    { 
     // In order to be able to create migrations and update database: 
     if (!options.IsConfigured) 
     { 
      options.UseSqlServer("YourLocalConnectionStringShouldBeHere"); 
     } 
     base.OnConfiguring(options); 
    } 
    #endregion 

    #region Model Creating 

    protected override void OnModelCreating(ModelBuilder modelBuilder) 
    { 
     // Your Model Crerating Stuff 
    } 

    #endregion Model Creating 
} 

3rd.- THÊM CƯ:

Đến thư mục dự án và từ cmd, gõ:

dotnet ef migrations add [NameOFYourMigrationGoesHere] -c YourContext 

Chú ý: Đừng quên add tạo file kiểm soát nguồn, điều đó không được thực hiện tự động kỳ diệu

4rd.- CẬP NHẬT DB của bạn: 4.a.- Trong docker -> Bạn có thể chạy dự án (hoàn toàn bằng Docker) và việc di chuyển sẽ được áp dụng khi sử dụng Ngữ cảnh đầu tiên.

lưu ý: (Nó sẽ sử dụng chuỗi kết nối cấu hình cho môi trường đó)

4.b.- DB địa phương của bạn -> Chỉnh sửa chuỗi kết nối hardcoded trong ConfigurationContext.OnConfigure và chạy (từ console cmd) :

dotnet ef database update --context ConfigurationContext 

Tôi hy vọng nó sẽ giúp bạn.

Juan

+0

'Microsoft.EntityFrameworkCore.Tools.DotNet' và' OnConfiguring' là tính năng EF7. bạn không cung cấp giải pháp cho EF6.1.3. – Shimmy

1

dự án EF6 của bạn phải cung cấp triển khai IDbContextFactory. Các công cụ dòng lệnh của EF6 sẽ tìm và sử dụng việc triển khai đó để chúng có thể khởi tạo bối cảnh. Đây là một ví dụ.

public class SchoolContextFactory : IDbContextFactory<SchoolContext> 
    { 
    public SchoolContext Create() 
    { 
     return new EF6.SchoolContext("Server=(localdb)\\mssqllocaldb;   Database=EF6MVCCore;Trusted_Connection=True;MultipleActiveResultSets=true"); 
    } 
    } 

Trong tệp Startup.cs của dự án Core, hãy thiết lập ngữ cảnh EF6 cho tiêm phụ thuộc (DI) trong ConfigureServices. Các đối tượng ngữ cảnh EF nên được sắp xếp theo thời gian cho mỗi yêu cầu.

public void ConfigureServices(IServiceCollection services) 
{ 
    // Add framework services. 
    services.AddMvc(); 
    services.AddScoped<SchoolContext>(_ => new SchoolContext(Configuration.GetConnectionString("DefaultConnection"))); 
} 

Trong bảng điều khiển quản lý gói (PMC) cho cả hai dự án, chạy lệnh Install-Package Entityframework.

Trong dự án thư viện lớp, tạo lớp mô hình dữ liệu và lớp ngữ cảnh và triển khai IDbContextFactory.

Trong PMC cho dự án thư viện lớp, hãy chạy lệnh Bật-di chuyển và Thêm di chuyển ban đầu. Nếu bạn đã thiết lập dự án ASP.NET Core làm dự án khởi động, hãy thêm -StartupProjectName EF6 vào các lệnh này. trong Startup.cs, đăng ký ngữ cảnh cho DI trong appsettings.json, thêm chuỗi kết nối.

Để biết thêm thông thăm

https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6

0

Vấn đề là EF 6 di cư là không quen với việc làm việc với các định dạng csproj mới.

Điều gì đã giúp tôi là dự án this.

Kiểm tra hướng dẫn trên trang chính của trang.

Nói chung, bạn chỉ cần thêm gói vào dự án của bạn theo cách được hướng dẫn và việc di chuyển sẽ hoạt động.

Các vấn đề liên quan