MVC3 EF4 在保存时复制外键对象
我使用 MVC3 和 EF4 代码优先。我有以下模型:
public class Order {
public int ID { get; set; }
[Required]
public float Price { get; set; }
[Required]
public int PayMethodId { get; set; }
public PayMethod PayMethod { get; set; }
public int? SpecificEventId { get; set; }
public SpecificEvent SpecificEvent { get; set; }
public int? SeasonalTicketId { get; set; }
public SeasonalTicket SeasonalTicket { get; set; }
}
当我尝试使用 specicEventId = 2
和 specicEvent = X
保存 Order 对象时,会创建一个新的 SpecificEvent
对象在数据库中,即使数据库中已经存在 ID 为 2 的特定事件 X。当我尝试使用 specicEventId = 2
和 specicEvent = null
时,出现数据验证错误。
我做错了什么?我希望 SpecificEvent
和 SeasonalTicket
可为空,并且我不希望 EF4 创建这些的新实例每当我保存“订单”时,数据库中的对象。
更新
这是我在数据库中保存Order
的代码:
public void SaveOrder(Order order)
{
Order fromDb = null;
// If editing an existing object.
if ((fromDb = GetOrder(order.ID)) != null)
{
db = new TicketsDbContext();
db.Entry(order).State = System.Data.EntityState.Modified;
db.SaveChanges();
}
// If adding a new object.
else
{
db.orders.Add(order);
db.SaveChanges();
}
}
当我保存时,我确实到达了else
子句。
I'm using MVC3 with EF4 code-first. I have the following model:
public class Order {
public int ID { get; set; }
[Required]
public float Price { get; set; }
[Required]
public int PayMethodId { get; set; }
public PayMethod PayMethod { get; set; }
public int? SpecificEventId { get; set; }
public SpecificEvent SpecificEvent { get; set; }
public int? SeasonalTicketId { get; set; }
public SeasonalTicket SeasonalTicket { get; set; }
}
When I try to save an Order object with specificEventId = 2
and specificEvent = X
, a new SpecificEvent
object is created in the DB, even though there's already a specific event X with ID 2 in the DB. When i try with specificEventId = 2
and specificEvent = null
I get a data validation error.
What am I doing wrong? I want SpecificEvent
and SeasonalTicket
to be nullable, and I don't want EF4 to create a new instance of these objects in the DB whenever I save 'Order'.
Update
This is my code for saving Order
in the DB:
public void SaveOrder(Order order)
{
Order fromDb = null;
// If editing an existing object.
if ((fromDb = GetOrder(order.ID)) != null)
{
db = new TicketsDbContext();
db.Entry(order).State = System.Data.EntityState.Modified;
db.SaveChanges();
}
// If adding a new object.
else
{
db.orders.Add(order);
db.SaveChanges();
}
}
When I save, I do reach the else
clause.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
真正的问题是,您从哪里获得 X 的实例? EF 似乎不知道此实例。您要么需要通过 EF 获取已经存在的 SpecificEvent 并使用它返回的代理来设置您的导航属性,要么告诉 EF “附加”X,以便它知道您的意图是什么。据 EF 所知,您似乎正在尝试向其发送一个具有冲突 ID 的新实例,因此它正确地发出了错误。
The real question is, where did you get the instance of X from? It appears as though EF has no knowledge of this instance. You either need to fetch the already existing SpecificEvent through EF and use the proxy it returns to set your navigation property, or else tell EF to "attach" X, so that it knows what your intent is. As far as EF knows, it appears, you are trying to send it a new instance with a conflicting Id, so it is properly issuing the error.