我如何实现简单的“复杂类型”?在Entity Framework Core 2 / C#中? [英] How do I implement a simple "complex type" in Entity Framework Core 2/C#?

查看:110
本文介绍了我如何实现简单的“复杂类型”?在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< T>

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆