我正在尝试尝试使用EF Core code first
来构建下面两个模型之间的架构和关系
public class Status
{
public int Id { get; set; }
public string Status { get; set; }
}
public class Order
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Status status { get; set; }
}
任何人都可以建议是否需要对上述模型(状态和顺序)进行任何更改以获取如下查询结果
OrderID description status
1 test1 approved
2 test2 rejected
答案 0 :(得分:1)
将导航属性“状态”设置为虚拟属性,例如:
public class Status
{
public int Id { get; set; }
public string Status { get; set; }
}
public class Order
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int StatusId { get; set; }
public virtual Status status { get; set; }
}
然后您可以在显示时对每一行进行Order.Status.Status
答案 1 :(得分:1)
我在下面编写代码并获得下一个结果。
1d7336aa-c144-4ca4-d2de-08d76cae93ab说明已批准
public static void Main()
{
ShowData(AddData());
}
public static void ShowData(Guid orderId)
{
using var context = new OrderContext();
Console.WriteLine(context.Orders
.Where(x => x.Id == orderId)
.First()
.ToString());
}
public static Guid AddData()
{
using var context = new OrderContext();
var status = new Status()
{
StatusName = "approved"
};
var order = new Order()
{
Description = "Description",
Name = "Name",
OrderStatus = status
};
context.Orders.Add(order);
context.SaveChanges();
return order.Id;
}
public class Status
{
public int Id { get; set; }
public string StatusName { get; set; }
public override string ToString()
{
return $"{StatusName}";
}
}
public class Order
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual Status OrderStatus { get; set; }
public override string ToString()
{
return $"{Id} {Description} {OrderStatus}";
}
}
我认为除了向OrderStatus添加虚拟修改器外,您什么都不要更改。