Phi*_*hil 12 c# entity-framework spatial ef-core-2.2
我正在尝试使用 EF 核心 2.2 构建带有空间对象的数据库,但在尝试创建数据库迁移时遇到问题。使用https://docs.microsoft.com/en-us/ef/core/modeling/spatial,特别是:
class Country
{
public int CountryID { get; set; }
public string CountryName { get; set; }
// Database includes both Polygon and MultiPolygon values
public IGeometry Border { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用此创建迁移,则会出现以下错误:
属性“Country.Border”属于接口类型(“IGeometry”)。如果它是导航属性,则通过将其转换为映射实体类型来手动配置此属性的关系,否则使用 NotMappedAttribute 或“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略该属性。
同样,如果我将其更改为 Geometry 类型,则会得到:
无法映射属性“Geometry.UserData”,因为它属于“object”类型,它不是受支持的基本类型或有效的实体类型。显式映射此属性,或使用 '[NotMapped]' 属性或使用 'OnModelCreating' 中的 'EntityTypeBuilder.Ignore' 忽略它。
我不知道我的对象是点、线还是多边形,所以它必须是通用的。我如何在我的结构中表示它?另外我看到有些地方说我需要添加以下代码:
public class MyDBContextFactory : IDesignTimeDbContextFactory<MyDBContext>
{
public MyDBContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<MyDBContext>();
builder.UseSqlServer(cnnString, x => x.UseNetTopologySuite());
return new MyDBContext(builder.Options);
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到错误:
“SqlServerDbContextOptionsBuilder”不包含“UseNetTopologySuite”的定义,并且找不到接受“SqlServerDbContextOptionsBuilder”类型的第一个参数的可访问扩展方法“UseNetTopologySuite”(您是否缺少 using 指令或程序集引用?)
即使我安装了 nuget 包
Tia*_*lva 23
Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite 2) 配置您的数据库以使用 NetTopologySuite(要编辑的代码通常在 中
StartUp.ConfigureServices())。只需, x => x.UseNetTopologySuite()在options.UseSqlServer括号内添加
所以它看起来像这样:
services.AddDbContext<ManagerContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection"),
x => x.UseNetTopologySuite()
)
);
Run Code Online (Sandbox Code Playgroud)
我不必using在文件中添加 a ,因为我已经引用了,仅供参考,Microsoft.EntityFrameworkCore如果您需要它。
如果您在安装 NuGet 包后仍收到参考错误,请转到管理 NuGet 包并检查它是否在已安装列表中,以及是否清理并重建您的解决方案并重新启动 Visual Studio,这可能会有所帮助。
| 归档时间: |
|
| 查看次数: |
4573 次 |
| 最近记录: |