【问题标题】:How do I create a Breeze entity of a derived class?如何创建派生类的 Breeze 实体?
【发布时间】:2014-04-12 06:20:11
【问题描述】:

在具有简单 TPH 继承结构的微风模型中,我无法弄清楚如何正确创建子类型的新微风实体。

例如:

public class Vehicle
{
    public string Name { get; set; }
    public byte VehicleType { get; set; }
}

public class Car : Vehicle
{
    // Some extra properties
}

鉴别器不遵循使用字符串值来确定类的父级的标准实体框架方法,因此我们使用一些 EF FluentAPI 来修改元数据:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Vehicle>()
                .Map<Car>(m => m.Requires("VehicleType").HasValue((byte)1));
}

以下代码的默认行为似乎是在 Breeze 中创建一个合适的实体,但从未设置鉴别器值,因此将更改保存到服务器会导致持久化 Vehicle 类型的对象而不是 Car。

var group = breezeManager.createEntity("Car", {
    Name: 'My car'
    // If the line below is omitted, VehicleType is saved
    // to the server with a value of 0
    ,VehicleType: 1
});
breezeManager.saveChanges();

虽然这似乎是关于 Breeze 客户端组件的行为的问题,但可能与我们使用 Breeze 存储到一个不通过实体框架访问的数据库有关(我们只是生成 EF 元数据供微风消耗)。

我不认为客户端代码应该担心我们的继承鉴别器的实现,所以希望在我们创建每个对象时可以手动设置值。

正确存储鉴别器值的最佳方法是什么?

【问题讨论】:

    标签: breeze


    【解决方案1】:

    我能够验证带有字节鉴别器值的 TPH 是否有效。我注意到我的模型和你的模型之间的唯一区别是我没有在我的模型中指定 byte 属性。

    public abstract class Airplane
    {
        public int Id { get; set; }
        public string Name { get; set; }
        //public byte AirplaneType { get; set; }  //Not required
    }
    
    public class Boeing : Airplane
    {
        public string Model { get; set; }
    }
    

    在 Fluent API 中指定鉴别器足以让 EF 保存正确的值。

    【讨论】:

    • 谢谢。没有意识到这是可选的,但没有它,一切都会按照你的建议完美运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 2013-11-16
    • 2014-09-29
    • 2023-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多