【问题标题】:Mapping fragment error TimeZoneInfo Object to db EF Code First将片段错误 TimeZoneInfo 对象映射到 db EF Code First
【发布时间】:2015-11-09 20:15:59
【问题描述】:

我正在使用带有 RadScheduleView 的 Teleriks WPF UI,并且在将自定义约会类映射到数据库时遇到了问题。我使用的是代码优先方法,当我创建自定义资源时一切正常,但自定义约会类失败。

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

"\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"

这是我的bokning课

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 类可以正常工作。但是bokning课给我带来了麻烦。我认为从 Appointment 类映射 TimeZone 属性存在问题。在数据库中创建了 Bokning 表,但并非所有列都在那里。我认为它崩溃导致 TimeZone 属性是 TimeZoneInfo 对象。

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

编辑:我可能用了map这个词并且映射错了

【问题讨论】:

    标签: c# wpf entity-framework telerik dbcontext


    【解决方案1】:

    添加

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

    到 onModelCreating

    使它忽略 timezone 属性,这很好。现在可以使用了

    【讨论】:

    • 但是您将丢失时区,因为这不会将时区属性/属性添加到您的数据库表中。到目前为止,我还没有找到更好的解决方案,然后将时区存储为字符串。
    【解决方案2】:

    只需将TimeZoneInfo.Id 属性保存在您的数据库中即可。然后通过TimeZoneInfo.FindSystemTimeZoneById()方法获取TimeZoneInfo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-26
      相关资源
      最近更新 更多