2017-01-13 63 views
2

我已經使用.Net Core 1.1和EF Core創建了一個Web API。後端是一個MySQL數據庫,因此我在project.json文件中包含了「MySql.Data.EntityFrameworkCore」:「7.0.6-IR31」依賴關係中的在.Net核心/ EF核心模型(MySQL後端)中被忽略的數據註釋

我在我的項目中有一個簡單的模型。我試圖將我的模型中的列映射到現有數據庫中的列。所以我使用數據註釋。我有這樣的:

using System; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 

namespace PropWorxAPI.Models 
    { 
     [Table("files")] 
     public partial class File 
     { 
      [Key] 
      [Column("file_id")] 
      public int FileId { get; set; } 
      [Required] 
      [Column("file_num")] 
      [MaxLength(50)] 
      public string FileNum { get; set; } 
      [MaxLength(255)] 
      public string Description { get; set; } 
     } 
    } 

我想物業FILEID映射到數據庫到現場的file_id。但是,當我運行該項目時,它抱怨數據庫中沒有字段FileId。這幾乎就像我的列映射註釋被完全忽略。有任何想法嗎?謝謝...

以防萬一,我包括我下面的project.json文件:

{ 
    "dependencies": { 
    "MySql.Data.EntityFrameworkCore": "7.0.6-IR31", 
    "Microsoft.AspNetCore.Mvc": "1.1.0", 
    "Microsoft.AspNetCore.Routing": "1.1.0", 
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0", 
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0", 
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0", 
    "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0", 
    "Microsoft.Extensions.Configuration.Json": "1.1.0", 
    "Microsoft.Extensions.Logging": "1.1.0", 
    "Microsoft.Extensions.Logging.Console": "1.1.0", 
    "Microsoft.Extensions.Logging.Debug": "1.1.0", 
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0", 
    "Microsoft.NETCore.App": { 
     "version": "1.1.0", 
     "type": "platform" 
    } 
    }, 

    "tools": { 
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final" 
    }, 

    "frameworks": { 
    "netcoreapp1.0": { 
     "imports": [ 
     "dotnet5.6", 
     "portable-net45+win8" 
     ] 
    } 
    }, 

    "buildOptions": { 
    "emitEntryPoint": true, 
    "preserveCompilationContext": true 
    }, 

    "runtimeOptions": { 
    "configProperties": { 
     "System.GC.Server": true 
    } 
    }, 

    "publishOptions": { 
    "include": [ 
     "wwwroot", 
     "**/*.cshtml", 
     "appsettings.json", 
     "web.config" 
    ] 
    }, 

    "scripts": { 
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] 
    } 
} 
+0

我在使用實體框架6數據訪問層的ASP.NET webforms中遇到了同樣的問題。我會通過控制檯運行器測試數據訪問層,並通過輸出通過context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s)創建的sql來進行測試,所有內容都會匹配。我將運行webform應用程序,SQL將嘗試使用類名稱作爲表名,並由於缺少表而失敗。我從數據註釋變成了流利的API。現在一切正常。我想你可能會想到這是一個EntityFrameworkCore問題。 – Rourke

回答

2

我有這個問題太,我能找到的是使用流暢的API定義映射的唯一解決方案而不是註釋。例如。

builder.Entity<Product>().ToTable("product").HasKey(m => m.Id); 
builder.Entity<Product>().Property(p => p.DateAdded).HasColumnName("date_added"); 
builder.Entity<Product>().Property(p => p.ImageUrl).HasColumnName("image_url"); 

希望很快可以支持註釋。

0

我想避免使用Fluent API,因爲我更願意在實際屬性上處理列映射,所以我使用反射構建了自己的列映射。

請參閱相關部件的OnModelCreating函數中的代碼。

注意我正在使用MySql並且代碼未經100%測試。

using System.ComponentModel.DataAnnotations.Schema; 
using Microsoft.EntityFrameworkCore; 
using MySQL.Data.EntityFrameworkCore.Extensions; 
using System.Reflection; 

namespace MyProject 
{ 
    public class DatabaseContext : DbContext 
    { 
     private string _connectionString; 

     public DbSet<Entity> Entities { get; set; } 

     public DatabaseContext(string connectionString) 
     { 
      _connectionString = connectionString; 
     } 

     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
     { 
      optionsBuilder.UseMySQL(_connectionString); 
     } 

     protected override void OnModelCreating(ModelBuilder modelBuilder) 
     { 
      // I couldn't get attribute mapping to work, so I 
      // had to do the mapping programmatically myself. 
      // Ideally we'd remove this code an rely on the 
      // built-in code to handle it, but we need to wait 
      // for MySql to support mapping in EF Core. 

      var type = typeof(Entity); 
      var properties = type 
       .GetTypeInfo() 
       .GetProperties(BindingFlags.Public | 
           BindingFlags.Instance | 
           BindingFlags.DeclaredOnly); 

      foreach (var property in properties) 
      { 
       System.Console.WriteLine(property.Name); 

       var ignore = property.GetCustomAttribute<NotMappedAttribute>(); 
       if (ignore != null) 
       { 
        modelBuilder 
         .Entity(type) 
         .Ignore(property.Name); 

        continue; 
       } 

       var column = property.GetCustomAttribute<ColumnAttribute>(); 

       if (column == null) 
       { 
        modelBuilder 
         .Entity(type) 
         .Property(property.Name) 
         .HasColumnName(property.Name); 

        continue; 
       } 

       modelBuilder 
        .Entity(type) 
        .Property(property.Name) 
        .HasColumnName(column.Name); 

      } 
     } 
    } 
}