我的应用程序中有关于数据合同的问题。
我的数据库中有5个表,我需要其中3个作为数据类型。所以我创建了一个类,并将所有必需的表解析为[Datacontract]
下的类。
这是代码
[DataContract]
public class CustomerDetail
{
[DataMember]
public int CustomerID { get; set; }
[DataMember]
public string CustomerName { get; set; }
[DataMember]
public long ContactNumber { get; set; }
[DataMember]
public string Email { get; set; }
[DataMember]
public string Address { get; set; }
[DataMember]
public string Password { get; set; }
}
[DataContract(Name = "CustomerDetail")]
public class CustomerAccount
{
[DataMember]
public int AccountId { get; set; }
[DataMember ]
public short PinNo { get; set; }
[DataMember (Name="CustomerID")]
public int CustomerID { get; set; }
[DataMember]
public int AccountTypeId { get; set; }
[DataMember]
public float Amount { get; set; }
}
[DataContract(Name = "CustomerDetail")]
public class TransactionDetail
{
[DataMember]
public int TransactionId { get; set; }
[DataMember (Name ="CustomerID")]
public int CustomerId { get; set; }
[DataMember]
public int AccountId { get; set; }
[DataMember]
public int TransactiopnTypeId { get; set; }
[DataMember]
public float AmountAfterTransaction { get; set; }
}
我也使用了属性,但是我得到了无效的数据合同异常.. PLZ帮助我解决这个问题。
答案 0 :(得分:1)
我认为这是因为您在数据合同中使用了Name参数。您似乎已经剪切并粘贴了这些属性。
只需从DataContract和DataMember属性中删除所有“Name”参数即可。它们对于你正在做的事情并不是必需的,并且会给你带来麻烦。
答案 1 :(得分:1)
问题是您为多个对象使用相同的名称“CustomerDetails”。尝试使用此代码代替您的代码(注意:我刚删除了两个Name = CustomerDetail属性)。
[DataContract]
public class CustomerDetail
{
[DataMember]
public int CustomerID { get; set; }
[DataMember]
public string CustomerName { get; set; }
[DataMember]
public long ContactNumber { get; set; }
[DataMember]
public string Email { get; set; }
[DataMember]
public string Address { get; set; }
[DataMember]
public string Password { get; set; }
}
[DataContract]
public class CustomerAccount
{
[DataMember]
public int AccountId { get; set; }
[DataMember]
public short PinNo { get; set; }
[DataMember(Name = "CustomerID")]
public int CustomerID { get; set; }
[DataMember]
public int AccountTypeId { get; set; }
[DataMember]
public float Amount { get; set; }
}
[DataContract]
public class TransactionDetail
{
[DataMember]
public int TransactionId { get; set; }
[DataMember(Name = "CustomerID")]
public int CustomerId { get; set; }
[DataMember]
public int AccountId { get; set; }
[DataMember]
public int TransactiopnTypeId { get; set; }
[DataMember]
public float AmountAfterTransaction { get; set; }
}