POCO类编码第一个数据库?

时间:2014-05-15 07:47:03

标签: c# entity-framework

我有从xsd.exe生成的POCO类,现在我想将这些类转换为Entity Framework代码的第一个数据库。

我试图生成代码第一个数据库我得到主键不包含的错误,如果我在该类中添加自定义主键列,则生成数据库。

当我尝试反序列化该类以使用该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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

当您使用代码优先技术创建数据库时会发生此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 属性。)