我如何实现一个简单的“复杂类型"?在 Entity Framework Core 2/C# 中? [英] How do I implement a simple "complex type" in Entity Framework Core 2/C#?
问题描述
我第一次使用 Entity Framework 和 .Net Core 2.0(我对 C# 也很陌生,但我从版本 1 开始就一直在使用传统的 .Net Framework 和 VB ......所以我我不是 .Net 开发的新手),而且我在创建数据库时遇到了问题.
I'm using Entity Framework and .Net Core 2.0 for the first time (I'm also pretty new to C#, but I've been using the traditional .Net Framework & VB since version 1... so I'm no newbie to .Net development), and I've already run into a problem creating my database.
以这个简单的场景为例:我想存储一些关于一些电动泵的信息.其中两个属性是最小/最大类型范围,因此我将它们实现为一个简单的类,因此:
Take this simple scenario: I want to store some information about some electric pumps. Two of the properties are a min/max type range, so I've implemented these as a simple class, thus:
public class Pump
{
[Key]
public int pumpId { get; set; }
public string pumpName { get; set; }
public int pumpControlChannel { get; set; }
public MinMax normalCurrent { get; set; }
public MinMax normalFlowRate { get; set; }
}
[ComplexType]
public class MinMax
{
public int min { get; set; }
public int max { get; set; }
}
如您所见,我尝试了 [ComplexType]
装饰器,但无济于事.
As you can see, I've tried the [ComplexType]
decorator, to no avail.
无论如何,现在创建一个简单的 DBContext 类来管理我的 Pumps 类.我正在使用 Sqlite:
Anyway, now create a dead simple DBContext class to manage my Pumps class. I'm using Sqlite:
public class EFDB : DbContext
{
public DbSet<Pump> pumps { get; private set; }
private static DbContextOptions GetOptions(string connectionString)
{
var modelBuilder = new DbContextOptionsBuilder();
return modelBuilder.UseSqlite(connectionString).Options;
}
public EFDB(string connectionString) : base(GetOptions(connectionString)) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
try
{
// modelBuilder.ComplexType<MinMax>(); // ComplexType not recognised
base.OnModelCreating(modelBuilder);
}
catch (Exception ex)
{
System.Diagnostics.Debugger.Break();
}
}
}
最后一个简单的静态类来调用它(我将它嵌入到一个更大的程序中...要复制这个问题,你可以将代码行粘贴到 program.cs 中):
and lastly a simple static class to call it (I embeded it in a bigger program... to duplicate this problem you could just stick the code lines into program.cs):
public static class TryMe
{
public static void MakeMeFail()
{
using (var db = new EFDB("FileName=C:\temp\test_effail.db"))
{
try
{
db.Database.EnsureCreated();
}
catch (Exception ex)
{
System.Diagnostics.Debugger.Break(); // If we hit this line, it fell over
}
}
System.Diagnostics.Debugger.Break(); // If we hit this line, it worked.
}
}
只需调用TryMe.MakeMeFail()
,代码在db.Database.EnsureCreated()
处失败.
Just call TryMe.MakeMeFail()
, the code fails at db.Database.EnsureCreated()
.
从我读过的所有内容来看,[ComplexType]
应该做我想做的事……但事实并非如此.我也找不到 modelBuilder.ComplexType
任何地方.
From everything I've read, [ComplexType]
should do what I want... but it Just Doesn't. Nor can I find modelBuilder.ComplexType<T>
anywhere.
这可能只是我遗漏的图书馆参考资料...?上面的代码使用了以下内容:
It may just be a library reference I'm missing...? The above code uses the following:
using System;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
但是,我在任何地方都找不到任何文档/示例显示哪些库需要引用!
However, NONE of the documentation/examples I can find anywhere show which libraries need referencing!
提前致谢.
[PS:向已经看到这个问题的人道歉,我使用的是 EF Core 2.0,而不是 EF6]
[PS: Apologies to those who already saw this question, I'm using EF Core 2.0, NOT EF6]
推荐答案
典型的...总是这样,不是吗?发帖 5 分钟后,您会发现自己问题的答案....
Typical... it's always the way, isn't it? 5 minutes after posting, you discover the answer to your own question....
在这种情况下,答案可以在这里找到:
The answer, in this case, can be found here:
https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities
EF Core 将这种实体称为拥有的"实体,而不是复杂类型".
EF Core calls this sort of entity an "owned" entity, rather than a "complex type".
只需将这些行添加到OnModelCreating"即可解决问题:
Simply adding these lines to `OnModelCreating' fixed the issue:
modelBuilder.Entity<Pump>().OwnsOne(p => p.normalCurrent);
modelBuilder.Entity<Pump>().OwnsOne(p => p.normalFlowRate);
数据库现在创建了(我认为是正确的,我还没有验证).
The database now creates (correctly, I think, I haven't verified that yet).
这篇关于我如何实现一个简单的“复杂类型"?在 Entity Framework Core 2/C# 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!