gpt4 book ai didi

c# - 无法确定导航属性 EF Core 表示的关系

转载 作者:行者123 更新时间:2023-12-04 07:19:54 25 4
gpt4 key购买 nike

我有这些类作为我的域模型:

public class Ticket
{
public Guid Id { get; set; }
public Guid ParentId { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public string TicketStatus { get; set; }
public User SenderUserId { get; set; }
public User ReciverrUserId { get; set; }
public DateTime SubmitDate { get; set; }
}

public class User
{
public Guid Id { set; get; }
public DateTime SubmitDate { set; get; }

public string Fullname { set; get; }
public string Permission { set; get; }
public string Role { set; get; }//admin/agent/merchant
public string Password { set; get; }

public string NationalCode { set; get; }
public string Email { set; get; }
public string Telephone { set; get; }
public string Mobile { set; get; }
public string Address { set; get; }

public CountrySection Provience { set; get; }
public CountrySection City { set; get; }
//public Department Department { set; get; }

public CountrySection Town { set; get; }

public ICollection<Merchant> Stores { set; get; }
public ICollection<Invoice> Invoices { set; get; }
public ICollection<Ticket> Tickets { set; get; }
}
当我添加迁移时,我收到此错误:

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

最佳答案

我在 Ticket 类中看到 2 个用户属性,但在 User 中只有一个 Ticket 集合,尝试通过添加另一个来修复它

public class Ticket
{
public Guid Id { get; set; }
.....
public int SenderUserId { get; set; }

[ForeignKey(nameof(SenderUserId ))]
[InverseProperty(nameof(User.SenderUserTickets))]
public User SenderUser { get; set; }

public int ReciverrUserId { get; set; }

[ForeignKey(nameof(ReciverrUserId))]
[InverseProperty(nameof(User.ReciverrUserTickets))]
public User ReciverrUser { get; set; }


public class User
{
public Guid Id { set; get; }
.....
[InverseProperty(nameof(Ticket.SenderUser ))]
public ICollection<Ticket> SenderUserTickets { set; get; }
[InverseProperty(nameof(Ticket.ReciverrUser ))]
public ICollection<Ticket> ReciverrUserTickets { set; get; }
}
抱歉,我正在使用属性,因为我喜欢将所有内容都放在一个地方。您可以将它们翻译成流畅的 api。我也添加了几个 Id,但它是可选的,EF 可以创建阴影。

关于c# - 无法确定导航属性 EF Core 表示的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68571826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com