如何在 Entity Framework Core 2/C# 中实现一个简单的“复杂类型”?

Ade*_*Ade 5 c# entity-framework-core .net-core

我第一次使用 Entity Framework 和 .Net Core 2.0(我对 C# 也很陌生,但我从版本 1 开始就一直在使用传统的 .Net Framework 和 VB ......所以我不是新手到 .Net 开发),并且我已经遇到了创建数据库的问题。

以这个简单的场景为例:我想存储一些关于一些电动泵的信息。其中两个属性是最小/最大类型范围,因此我将它们实现为一个简单的类,因此:

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; }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我尝试了[ComplexType]装饰器,但无济于事。

无论如何,现在创建一个简单的 DBContext 类来管理我的 Pumps 类。我正在使用 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();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

最后一个简单的静态类来调用它(我将它嵌入到一个更大的程序中......要复制这个问题,你可以将代码行粘贴到 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.
    }
}
Run Code Online (Sandbox Code Playgroud)

只需调用TryMe.MakeMeFail(),代码就会失败db.Database.EnsureCreated()。

从我读过的所有内容来看,[ComplexType]应该做我想做的……但事实并非如此。我也找不到modelBuilder.ComplexType<T>任何地方。

它可能只是我缺少的图书馆参考资料......?上面的代码使用了以下内容:

using System;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
Run Code Online (Sandbox Code Playgroud)

但是,我在任何地方都找不到任何文档/示例显示哪些库需要引用!

提前致谢。

[PS:向已经看到这个问题的人道歉,我使用的是 EF Core 2.0,而不是 EF6]

Ade*_*Ade 6

典型的……总是这样,不是吗?发帖后 5 分钟,你就找到了自己问题的答案......

在这种情况下,可以在这里找到答案:

https://learn.microsoft.com/en-us/ef/core/modeling/owned-entities

EF Core 将此类实体称为“拥有的”实体,而不是“复杂类型”。

只需将这些行添加到“OnModelCreating”即可解决该问题:

modelBuilder.Entity<Pump>().OwnsOne(p => p.normalCurrent);
modelBuilder.Entity<Pump>().OwnsOne(p => p.normalFlowRate);
Run Code Online (Sandbox Code Playgroud)

数据库现在已创建(我认为正确,我尚未验证这一点)。