I have a Web Api 2 Service and a WPF OData v4 Client.
I have two tables( Teacher, Subject ) having One-to-Many relationship between them. I am trying to get Subjects against Teacher_Id.
Following is the code that I am using:
var Teacher = from c in Container.Teachers
select new
{
Name = c.Name,
FatherName = c.FatherName,
ContactNo = c.ContactNo,
Address = c.Address,
Religion = c.Religion,
CNIC = c.CNIC,
Status = c.Status,
UserName = c.UserName,
Subjects = c.Subjects.Where(s=> s.Teacher_Id == c.Teacher_Id).SingleOrDefault()
};
The code of My Model Classes is as Follows:
public partial class Subject
{
[Key]
public string Subject_Id { get; set; }
public string Subject_Name { get; set; }
public string Class_Id { get; set; }
public string Teacher_Id { get; set; }
public virtual Book Book { get; set; }
public virtual Class Class { get; set; }
public virtual Teacher Teacher { get; set; }
}
public partial class Teacher
{
public Teacher()
{
this.Subjects = new HashSet<Subject>();
}
[Key]
public string Teacher_Id { get; set; }
public string Name { get; set; }
public string FatherName { get; set; }
public string Address { get; set; }
public string ContactNo { get; set; }
public string Religion { get; set; }
public string CNIC { get; set; }
public string Status { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public virtual ICollection<Subject> Subjects { get; set; }
}
The problem is that I am getting the following exception:
An unhandled exception of type 'System.NotSupportedException' occurred in Microsoft.OData.Client.dll
Additional information: Constructing or initializing instances of the type <>f__AnonymousType1`9[System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,QuizSystemClient.RestApiOData.Models.Subject] with the expression c.Subjects.Where(s => (s.Teacher_Id == c.Teacher_Id)).SingleOrDefault() is not supported.
According to the following article, you could try to add the ForeignKey definition in the class “Subject” and then add some other settings in the WebApiConfig.cs.
Maybe you could try to use the “FirstOrDefault()” to get the code in your code. Whenever you use
SingleOrDefault, you clearly state that the query should result in at most a single result. I have created a demo as below and you could have a look.
List<Teacher> teachers = new List<Teacher> {
new Teacher { Name="T100", Teacher_Id= "T100",
Subjects = new List<Subject> {
new Subject { Subject_Id="S100", Subject_Name="S100", Teacher_Id="T100" },
new Subject { Subject_Id="S101", Subject_Name="S101", Teacher_Id="T100" },
new Subject { Subject_Id="S102", Subject_Name="S102", Teacher_Id="T100" },
} },
new Teacher { Name="T200", Teacher_Id= "T200",
Subjects = new List<Subject> {
new Subject { Subject_Id="S200", Subject_Name="S200", Teacher_Id="T200" },
new Subject { Subject_Id="S201", Subject_Name="S201", Teacher_Id="T200" },
new Subject { Subject_Id="S202", Subject_Name="S202", Teacher_Id="T200" },
} }
};
try
{
var resutl = from t in teachers
select new
{
TeacherName = t.Name,
Subjects = t.Subjects.Where(s => s.Teacher_Id == t.Teacher_Id).FirstOrDefault()
};
}
catch (Exception)
{
throw;
}
But, you said you couldn’t access subjects of a teacher, the code in the initial post seems just want to get one Subject of a teacher. If you want to get all the subjects of a teacher, you could just access the property Subjects of an instance of
a Teacher. If I’m wrong, please let me know what do you want to achieve.
Member
1 Points
8 Posts
OData V4 Client Code Generator handle One-to-Many Relationship using c# proxy Class
Oct 11, 2015 02:45 PM|Sameel|LINK
Hi,
I have a Web Api 2 Service and a WPF OData v4 Client.
I have two tables( Teacher, Subject ) having One-to-Many relationship between them. I am trying to get Subjects against Teacher_Id.
Following is the code that I am using:
The code of My Model Classes is as Follows:
The problem is that I am getting the following exception:
Please tell me How can I resolve this problem ?
OData webapi2
Star
7970 Points
1586 Posts
Re: OData V4 Client Code Generator handle One-to-Many Relationship using c# proxy Class
Oct 12, 2015 04:47 AM|Weibo Zhang|LINK
Hi Sameel,
According to the following article, you could try to add the ForeignKey definition in the class “Subject” and then add some other settings in the WebApiConfig.cs.
http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/entity-relations-in-odata-v4
I hope it’s useful to you.
Best Regard,
Weibo Zhang
OData webapi2
Member
1 Points
8 Posts
Re: OData V4 Client Code Generator handle One-to-Many Relationship using c# proxy Class
Oct 14, 2015 01:07 PM|Sameel|LINK
Hi Weibo Zhang,
I had tried it but still I am unable to access subjects of a teacher. Is there any other method?
OData webapi2
Star
7970 Points
1586 Posts
Re: OData V4 Client Code Generator handle One-to-Many Relationship using c# proxy Class
Oct 16, 2015 05:53 AM|Weibo Zhang|LINK
Hi Sameel,
Maybe you could try to use the “FirstOrDefault()” to get the code in your code. Whenever you use SingleOrDefault, you clearly state that the query should result in at most a single result. I have created a demo as below and you could have a look.
But, you said you couldn’t access subjects of a teacher, the code in the initial post seems just want to get one Subject of a teacher. If you want to get all the subjects of a teacher, you could just access the property Subjects of an instance of a Teacher. If I’m wrong, please let me know what do you want to achieve.
I hope it’s useful to you.
Best Regard,
Weibo Zhang
OData webapi2
Member
1 Points
8 Posts
Re: OData V4 Client Code Generator handle One-to-Many Relationship using c# proxy Class
Oct 16, 2015 08:29 AM|Sameel|LINK
Hi Weibo Zhang,
Thanx alot for your response :).
OData webapi2