首先使用 EF Core 代码在两个表之间进行映射
mapping between two tables using EF Core Code first
我正在尝试使用 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
使您的导航 属性 "Status" 虚拟 属性 如:
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
我在下面编写代码并得到下一个结果。
1d7336aa-c144-4ca4-d2de-08d76cae93ab Description approved
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 添加虚拟修改器外,您不应该更改任何内容。
我正在尝试使用 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
使您的导航 属性 "Status" 虚拟 属性 如:
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
我在下面编写代码并得到下一个结果。
1d7336aa-c144-4ca4-d2de-08d76cae93ab Description approved
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 添加虚拟修改器外,您不应该更改任何内容。