将片段错误 TimeZoneInfo 对象映射到 db EF Code First

Mapping fragment error TimeZoneInfo Object to db EF Code First

我将 Teleriks WPF UI 与 RadScheduleView 一起使用,但我无法将自定义约会 class 映射到数据库。我使用的是代码优先方法,当我创建自定义资源时一切正常,但自定义约会 class 失败。

我在编译时得到这个内部错误。

"\r\n(6,10) : error 3004: Problem in mapping fragments starting at line 6:No mapping specified for properties Bokning.TimeZone in Set Bokning.\r\nAn Entity with Key (PK) will not round-trip when:\r\n  Entity is type [TelerikWpfApp1.Bokning]\r\n"

这是我的书class

class Bokning : Appointment
{
    public Bokning() { }
    private int id;
    public int Id
    {
        get
        {
            return this.Storage<Bokning>().id;
        }

        set
        {
            var storage = this.Storage<Bokning>();
            if (storage.id != value)
            {
                storage.id = value;
                this.OnPropertyChanged(() => this.Id);
            }
        }
    }
    public override IAppointment Copy()
    {
        var customAppointment = new Bokning();
        customAppointment.CopyFrom(this);
        return customAppointment;
    }
    public override void CopyFrom(IAppointment other)
    {
        var customAppointment = other as Bokning;
        if (customAppointment != null)
        {
            this.Id = customAppointment.Id;
        }
        base.CopyFrom(other);
    }

}

这是我的背景

class DBContext : DbContext
{
    public DBContext(): base("ADO Solutions")
    {
    }
    public DbSet<Personal> Personal { get; set; }

    public DbSet<Jobb> Jobb { get; set; }

    public DbSet<Bokning> Bokning { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }


}

Personal 和 Jobb classes 可以正常工作。但是 bokning class 给我带来了麻烦。我认为从约会 class 映射 TimeZone 属性时出现问题。在数据库中创建了 Bokning table 但并非所有列都在那里。我认为它崩溃是因为 TimeZone 属性是一个 TimeZoneInfo 对象。

我想我需要一种方法将 TimeZoneInfo 对象映射为数据库中的字符串,但我什至不确定这是问题所在。

编辑:我可能用错了 map 和 mapping 这个词

添加

modelBuilder.Entity<Bokning>().Ignore(t => t.TimeZone);

到 onModelCreating

使其忽略时区 属性,这很好。现在可以使用了

只需将 TimeZoneInfo.Id 属性 保存在您的数据库中。然后用TimeZoneInfo.FindSystemTimeZoneById()方法得到TimeZoneInfo