如何在便携式类库中为EF Core添加模式注释

问题描述:

对于我正在研究的项目,我们编写了一个便携式PCL以包含将在多个项目之间共享的模型和其他代码。数据访问层位于解决方案的另一个项目中,但我想在PCL中使用EF数据注释以符合我们当前的约定。我在System.ComponentModel.DataAnnotations命名空间中的数据注释工作得很好,但未找到System.ComponentModel.DataAnnotations.Schema注释。如何在便携式类库中为EF Core添加模式注释

该引用被添加,我甚至尝试重新添加EF到PCL,但我没有任何运气。我读了另一篇文章,他们同时使用两个命名空间遇到了一个问题,但我甚至无法获得.Schema本身的工作。

这里是我的代码看起来像一个例子:

using Newtonsoft.Json; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; //VS shows that this is not being used 

namespace Shared.Models.User 
{ 
    public class BaseUser 
    { 
     [JsonProperty("id")] 
     [Key] 
     [Column("UserId", Order = 1)] // ***ColumnAttribute not found  
     public Guid Id { get; set; } 
     ... 
     [JsonProperty("fullName")] 
     [NotMapped] // ***NotMappedAttribute not found 
     public string FullName => String.Format("{0} {1}", FirstName, LastName); 
     ... 
    } 
} 

的PCL的目标是.NET 4.5,对.NET核心1.0,UWP和Xamarin平台。在Nuget Package Manager中,它显示前面提到的两个名称空间都已添加到项目中,但它们不会显示在解决方案资源管理器中。我曾尝试清理项目,构建和恢复软件包,但仍然没有任何结果。我没有看到任何理由,这不应该在便携式PCL中工作,但我错过了什么?

是否安装System.ComponentModel.Annotations NuGet软件包的帮助?如果没有,他们可能只是不适用于该类型的项目。

+0

不,这根本没有帮助。这是现在正在工作的那个,但是System.ComponentModel.Annotations.Schema是我遇到的问题之一。这也是我的想法,但想看看是否有其他人遇到过这种情况。 – dzlp

+0

确认。您需要定位至少[.NET标准](https://docs.microsoft.com/en-us/dotnet/standard/net-standard)1.3(没有等效的PCL配置文件) – bricelam