【问题标题】:POCO Classes to Code First Database?POCO 类到代码优先数据库?
【发布时间】:2014-07-03 13:01:05
【问题描述】:

我有从 xsd.exe 生成的 POCO 类,现在我想将这些类转换为实体框架代码优先数据库。

我尝试生成代码优先数据库,但出现未包含主键的错误,如果我在该类中添加自定义主键列,则会生成数据库。

当我尝试反序列化该类以使用该 POCO 类提取数据时,它会引发类型不匹配错误。

我需要一种方法将我的 POCO 类转换为数据库并反序列化该类以加载数据然后保存到数据库。

编辑

我得到的错误是“有一个错误反映类型 t_Response”。

以下是 DE 序列化代码。

XmlSerializer serializer = new XmlSerializer(typeof(t_Response));
                using (StreamReader reader = new StreamReader(file))
                {
                    t_Response obj_t_Response = (t_Response)serializer.Deserialize(reader);
                    t_ResponseDetails responseDetails = (t_ResponseDetails)obj_t_Response.Item;
                    if (responseDetails.Items.Count() > 0)
                    {
                        t_SearchItemInformationResponse searchItemInfoResp = (t_SearchItemInformationResponse)responseDetails.Items[0];

                        t_ItemDetails itemDetails = (t_ItemDetails)searchItemInfoResp.Item;
                        t_ItemDetail[] itemDetail = (t_ItemDetail[])itemDetails.ItemDetail;
                        if (itemDetail.Count() > 0)
                        {
                            t_City city = itemDetail[0].City;
                            t_Location[] location = itemDetail[0].LocationDetails;
                            t_Item item = itemDetail[0].Item;
                            t_HotelInformation hotelInformation = (t_HotelInformation)itemDetail[0].Item1;
                            //hotelInformation.AddressLines.
                            //hotelInformati
                        }
                    }                        
                }

响应类的类结构是

 public partial class t_Response
{
    public int Id { get; set; }
}



public partial class t_Response
{
    private object itemField;

    private string requestReferenceField;

    private string responseReferenceField;

    private string responseSequenceField;

    private System.DateTime timeStampField;

    private bool timeStampFieldSpecified;


    [System.Xml.Serialization.XmlElementAttribute("Errors", typeof(t_Errors), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
    [System.Xml.Serialization.XmlElementAttribute("ResponseDetails", typeof(t_ResponseDetails), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
    public object Item
    {
        get
        {
            return this.itemField;
        }
        set
        {
            this.itemField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string RequestReference
    {
        get
        {
            return this.requestReferenceField;
        }
        set
        {
            this.requestReferenceField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ResponseReference
    {
        get
        {
            return this.responseReferenceField;
        }
        set
        {
            this.responseReferenceField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(DataType = "integer")]
    public string ResponseSequence
    {
        get
        {
            return this.responseSequenceField;
        }
        set
        {
            this.responseSequenceField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public System.DateTime TimeStamp
    {
        get
        {
            return this.timeStampField;
        }
        set
        {
            this.timeStampField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool TimeStampSpecified
    {
        get
        {
            return this.timeStampFieldSpecified;
        }
        set
        {
            this.timeStampFieldSpecified = value;
        }
    }
}

【问题讨论】:

  • 嗨,欢迎来到堆栈溢出。请将代码和确切错误发送给我们,以便我们查明问题。
  • 添加自定义主键时类型不匹配。
  • 在POCO的主键上添加[XmlIgnore]注解。
  • 感谢 stefan,编辑问题的代码和错误
  • 感谢 AlexC 让我试试你的建议

标签: c# entity-framework


【解决方案1】:

这个Mismatch exception 发生在你曾经使用代码优先技术创建数据库,然后你add a new attribute to your class. (i.e Id in your case). In this case, database need to be updated (some migration tools etc). 或者你可以尝试一个新的类和东西。 (这次别忘了放置 ID 属性)。

【讨论】:

  • 感谢 zeeshan,类结构不是由 xsd 架构创建而是由 xsd 架构提供的,所以下次没有选项添加 id :)
  • @MuzaffarAli 不,我的意思是,尝试在您的模型类中创建 ID 字段,您的模式通过反序列化映射到该字段。 (或者我错过了什么?)
  • 我在添加 id 列后遇到反序列化问题,xmlignore 有帮助。
猜你喜欢
  • 1970-01-01
  • 2012-03-19
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2013-09-23
  • 1970-01-01
  • 2013-03-13
  • 1970-01-01
相关资源
最近更新 更多