在 Entity Framework Core 中使用 [ComplexType] [英] Using [ComplexType] in Entity Framework Core

查看:20
本文介绍了在 Entity Framework Core 中使用 [ComplexType]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 EF Core 数据模型中使用了我自己的类的属性.

I'm using a property of my own class inside EF Core data model.

public class Currency
{
    public string Code { get; set; }
    public string Symbol { get; set; }
    public string Format { get; set; }
}

[ComplexType]
public class Money
{
    public int? CurrencyID { get; set; }
    public virtual Currency Currency { get; set; }
    public double? Amount { get; set; }
}

public class Rate
{
    public int ID { get; set; }
    public Money Price = new Money();
}

我的问题是,当我尝试创建迁移时,EF Core 报告错误.

My problem is that when I try to create a migration, EF Core reports a error.

Microsoft.Data.Entity.Metadata.ModelItemNotFoundException: The entity type 'RentABike.Models.Money' requires a key to be defined.

如果我声明一个键,则会创建一个单独的Money"表,这不是我要找的.

If I declare a key, a separate table for "Money" is created, which is not what I'm looking for.

有没有办法在 EF Core 中使用 ComplexType 并将其全部放入一个表中?

Is there any way to use ComplexType in EF Core and put it all into a single table?

推荐答案

对复杂类型的支持目前处于积压状态 https://github.com/aspnet/EntityFramework/issues/246

Support for complex types is currently on the backlog https://github.com/aspnet/EntityFramework/issues/246

这篇关于在 Entity Framework Core 中使用 [ComplexType]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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