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. I'm also using a single model with 4 different views. 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:
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. Works without issues for the BasicOrder type.
@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:
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. 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. As long as I implement just the OrderBasic collection, I have no issues. When I add the second order model (OrderExtended) I receive the following EF related error:
An exception of type 'System.ArgumentNullException' occurred in EntityFramework.dll but was not handled in user code. Additional information: Value cannot be 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. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.