[英]EF Code First multiple 0-to-many
I'm unable to add a second one-to-many (I'd prefer a zero-to-many)relationship to my class.... 我无法在班级中添加第二对多关系(我希望零对多关系)。
I'm also using the Entity First framework. 我也在使用Entity First框架。 I'm also using a single model with 4 different views.
我还使用具有4个不同视图的单个模型。 2 of them should have a second x-to-many relationship with class A, and the other 2 should have a one-to-many relationship with class B. Here's how my classes look:
其中两个应该与A类建立第二个x对多关系,另外两个应该与B类具有一对多的关系。这是我的类的外观:
public class Request
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
...
other properties
...
public virtual ICollection<OrderBasic> Orders { get; set; }
public virtual ICollection<OrderExtended> ExtendedOrders { get; set; }
public Request()
{
Orders = new List<OrderBasic>();
ExtendedOrders = new List<OrderExtended>();
}
}
public class OrderBasic
{
public int ID { get; set; }
...
specific OrderBasic properties
...
public int RequestRefId { get; set; }
[ForeignKey("RequestRefId")]
public virtual Request Request { get; set; }
}
public class OrderExtended
{
public int ID { get; set; }
...
specific OrderExtended properties
...
public int RequestRefId { get; set; }
[ForeignKey("RequestRefId")]
public virtual Request Request { get; set; }
}
View which renders a different PartialView depending on the specific type of order I want. 根据我想要的顺序的特定类型,呈现不同的PartialView的视图 。 Works without issues for the BasicOrder type.
对于BasicOrder类型,可以正常工作。
@model RFP_MVC.Models.Request
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<legend>General info:</legend>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
//Request Model properties...
<table class="table table-striped" id="orderTable">
<thead>
<tr>
<th>Material</th>
<th>Quantity</th>
<th>Unit</th>
<th></th>
</tr>
</thead>
<tbody id="OrderZone">
@foreach (var row in Model.ExtendedOrders)
{
Html.RenderPartial("~/Views/Orders/OrderExtendedCreate.cshtml", row);
}
</tbody>
</table>
<button class="btn btn-sm btn-success" type="button" id="addOrder">Add order</button>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
DbContext class: dbContext类:
namespace RFP_MVC.DAL
{
public class DatabaseContext : DbContext
{
public DbSet<Shipment> shipments { get; set; }
public DbSet<Request> requests { get; set; }
public DbSet<OrderBasic> basicOrders { get; set; }
public DbSet<OrderExtended> extendedOrders { get; set; }
}
}
I then generate 4 different views based on a propery requestType. 然后根据适当的requestType生成4个不同的视图。 Two views have a collection of OrderBasic, the two other views should implement OrderExtended I then use a partial view to render properties of the order model.
两个视图具有OrderBasic的集合,另外两个视图应实现OrderExtended,然后使用局部视图来呈现订单模型的属性。 As long as I implement just the OrderBasic collection, I have no issues.
只要我仅实现OrderBasic集合,就没有问题。 When I add the second order model (OrderExtended) I receive the following EF related error:
当我添加第二个订单模型(OrderExtended)时,我收到以下与EF相关的错误:
An exception of type 'System.ArgumentNullException' occurred in EntityFramework.dll but was not handled in user code.
EntityFramework.dll中发生类型为'System.ArgumentNullException'的异常,但未在用户代码中处理。 Additional information: Value cannot be null.
附加信息:值不能为null。
The problem appears when I try to display the Index of a certain View, even if it's that of a completely unrelated model (Shipment in this case). 当我尝试显示某个视图的索引时,即使是完全不相关的模型(在这种情况下为装运),也会出现问题。
Looks like I found the the culprit. 看来我找到了罪魁祸首。 My model contains a file upload:
我的模型包含文件上传:
[DataType(DataType.Upload)]
public HttpPostedFileBase Attachment { get; set; }
Which yielded the following error as soon as I tried to add a migration: 尝试添加迁移后,立即产生以下错误:
Value cannot be null.
Parameter name: entitySet
HttpPostedFileBase isn't supported by EF. EF不支持HttpPostedFileBase。 I'll have to add a property which just stores a reference to the file location, instead of storing the actual file in my DB.
我将不得不添加一个属性,该属性仅存储对文件位置的引用,而不是将实际文件存储在数据库中。 Something like this:
像这样:
[NotMapped]
[DataType(DataType.Upload)]
public HttpPostedFileBase Attachment{ get; set; } //file to pass to controller
public string FilePath { get; set; } //reference to file path
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.