MVC3 EF4 在保存时复制外键对象

发布于 2024-11-27 12:12:10 字数 1390 浏览 6 评论 0原文

我使用 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 = 2specicEvent = X 保存 Order 对象时,会创建一个新的 SpecificEvent 对象在数据库中,即使数据库中已经存在 ID 为 2 的特定事件 X。当我尝试使用 specicEventId = 2specicEvent = null 时,出现数据验证错误。

我做错了什么?我希望 SpecificEventSeasonalTicket 可为空,并且我不希望 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心舞飞扬 2024-12-04 12:12:10

真正的问题是,您从哪里获得 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文