如何在一个模型中首先在EF代码中添加两个属性?

发布于 2025-02-02 02:50:35 字数 523 浏览 3 评论 0原文

我想添加城市模型中的两个属性:

迁移后,此错误显示出来:

无法确定导航代表的关系 类型“ Icollection”类型的“城市”。要么手动配置 关系,或使用“ [未图表]”忽略此属性 属性或使用“ entityTypebuilder.ignore”中的“ on ModeLcreating”。

这是我的代码:

public class Order
{
    public virtual City FromCity { get; set; }
    public virtual City ToCity { get; set; }
}

public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}

I want to add two properties from the city model:

after migration this error shows up:

Unable to determine the relationship represented by navigation
'City.Orders' of type 'ICollection'. Either manually configure
the relationship, or ignore this property using the '[NotMapped]'
attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

here is my code :

public class Order
{
    public virtual City FromCity { get; set; }
    public virtual City ToCity { get; set; }
}

public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉徒眸意 2025-02-09 02:50:35

我想您的模型比FromCitytocity更复杂,因为我认为将这些信息存储在其他表中不是一个好主意。但是,在这种情况下,您可以使用继承。

默认情况下使用table-per-hierarchy(TPH)模式来映射在EF中的继承。 TPH在单个表中将所有类型的数据存储在层次结构中。

但是,对于您的情况,您可以拥有一个持有所有相关属性的基类。

public class CityBase
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
}

然后假设您按照您的方案需要两个实体:

public class FromCity : CityBase
{
    public virtual ICollection<Order> Orders { get; set; } = null!;
}
public class ToCity : CityBase
{
    public virtual ICollection<Order> Orders { get; set; } = null!;
}

订单实体:

public class Order
{
    public int Id { get; set; }
    public string OrderTitle { get; set; } = string.Empty;
    public virtual FromCity FromCity { get; set; } = null!;
    public virtual ToCity ToCity { get; set; } = null!;
}

此方法可以通过订单FromCity之间的一对多关系来解决您的问题。代码> tocity根据下图:

“关系图”

I suppose your model is more complicated than just FromCity and ToCity because I don't think it's a good idea to store such information in a different table. Yet, You can use inheritance in this case.

The table-per-hierarchy (TPH) pattern is used by default to map the inheritance in EF. TPH stores the data for all types in the hierarchy in a single table.

However, for your scenario, you can have a base class that holds all related attributes.

public class CityBase
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
}

Then suppose you need two entities as per your scenario:

public class FromCity : CityBase
{
    public virtual ICollection<Order> Orders { get; set; } = null!;
}
public class ToCity : CityBase
{
    public virtual ICollection<Order> Orders { get; set; } = null!;
}

And the order entity:

public class Order
{
    public int Id { get; set; }
    public string OrderTitle { get; set; } = string.Empty;
    public virtual FromCity FromCity { get; set; } = null!;
    public virtual ToCity ToCity { get; set; } = null!;
}

This approach can solve your problem with a One-to-Many relationship between Orders and FromCity, ToCity as per below diagram:

Relationship Diagram

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文