I'm trying to implement an sorting table in MVC with Razor but i'm having such a pain to make the sorting work with some fields which are non primitive. My class model is as follows
public class Turma
{
public int ID { get; set; }
public string Ano { get; set; }
[ForeignKey("Professor")]
public int Professor_ID { get; set; }
public virtual Professor Professor { get; set; }
[ForeignKey("Disciplina")]
public int Disciplina_ID { get; set; }
public virtual Disciplina Disciplina { get; set; }
}
public class Professor
{
public int ID { get; set; }
public string Nome { get; set; }
public string Email { get; set; }
public string Telefone { get; set; }
}
public class Disciplina
{
public int ID { get; set; }
public string Nome { get; set; }
}
I'm tring to, with an DbSet, sorting it like this:
turmas = turmas.OrderByDescending(s => s.Disciplina.Nome);
Is that even possible?
Related
I have been reading about Entity Framework during the pass few weeks. I came across TPT subject. I am a little bit confused and have difficulty to differentiate between TPT and Navigation. When should we choose one over another. Please take a look at the code below.
A. Navication
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public List<ContractEmployee> ContractEmployees { get; set; }
public List<PermanentEmployee> PermanentEmployees { get; set; }
}
public class ContractEmployee
{
public int HourlyWorked { get; set; }
public int HourlyPay { get; set; }
public Employee Employee { get; set; }
}
public class PermanentEmployee
{
public int AnnualSalary { get; set; }
public Employee Employee { get; set; }
}
B. TPT
[Table("Employee")]
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
}
[Table("ContractEmployee")]
public class ContractEmployee : Employee
{
public int HourlyWorked { get; set; }
public int HourlyPay { get; set; }
}
[Table("ContractEmployee")]
public class PermanentEmployee : Employee
{
public int AnnualSalary { get; set; }
}
I have a c# class as below
public class CreateStudent
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
and I need another class with following properties
public class EditStudent
{
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
public DateTime Date_of_Birth { get; set; }
}
I have repeated properties except that one field (Date_of_Birth) is added in the EditStudent Model.
Is there an option to reuse some of the properties from previous CreateStudent model
I am going to handle these data as Json objects in my front end angularjs based application
You could do this with a null-able property.
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
public DateTime? Date_of_Birth { get; set; }
}
This way you only have one Student model that can accommodate both use-cases.
you should be using inheritance feature here.
public class CreateStudent
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
public class EditStudent : CreateStudent
{
public DateTime Date_of_Birth { get; set; }
}
I have a class with nested subclasses:
public class listDevicesModel
{
public int totalCount { get; set; }
public Messages messages { get; set; }
public Devices devices { get; set; }
public class Entry
{
public string key { get; set; }
public object value { get; set; }
}
public class Detail
{
public List<Entry> entry { get; set; }
}
public class Devices
{
public List<Device> device { get; set; }
}
public class Device
{
public string #id { get; set; }
public string uuid { get; set; }
public string principal { get; set; }
public int blockReason { get; set; }
public int clientId { get; set; }
public string comment { get; set; }
public int compliance { get; set; }
public int countryCode { get; set; }
public int countryId { get; set; }
public string countryName { get; set; }
public string createdAt { get; set; }
public string currentPhoneNumber { get; set; }
public List<Detail> details { get; set; }
public int deviceCount { get; set; }
public string easLastSyncAttempt { get; set; }
public string easUuid { get; set; }
public string emailAddress { get; set; }
public string emailDomain { get; set; }
public bool employeeOwned { get; set; }
public string homeOperator { get; set; }
public int languageCountryId { get; set; }
public int languageId { get; set; }
public string lastConnectedAt { get; set; }
public string manufacturer { get; set; }
public bool mdmManaged { get; set; }
public int mdmProfileUrlId { get; set; }
public string model { get; set; }
public string name { get; set; }
public bool notifyUser { get; set; }
public string #operator { get; set; }
public int operatorId { get; set; }
public string platform { get; set; }
public string platformType { get; set; }
public int quarantinedStatus { get; set; }
public int regCount { get; set; }
public string regType { get; set; }
public string registeredAt { get; set; }
public string status { get; set; }
public int statusCode { get; set; }
public string userDisplayName { get; set; }
public string userFirstName { get; set; }
public string userLastName { get; set; }
public int userSource { get; set; }
public string userUUID { get; set; }
public int wipeReason { get; set; }
}
}
In my MVC razor view i try to access the data:
#model PostenNorge.Models.JsonObjectModels.listDevicesModel
<b>#Model.messages.message</b>
<br />
<br />
<ul>
#foreach(Device d in Model.devices.device)
{
<li>#d.name - #d.uuid - #d.currentPhoneNumber</li>
}
</ul>
On Device in the foreach i get "The type or namespace name "Device" could not be found".
How can i access the nested class types in my view?
You need to specify the nested name:
#foreach(listDevicesModel.Device d in Model.devices.device)
Or use implicit typing:
#foreach(var d in Model.devices.device)
Personally I'd avoid using nested classes here anyway, unless you really have to. Even if you do really have to, I'd rename the top-level class to follow normal naming conventions, i.e. make it start with a capital letter.
I've been working on this for a while now and i still haven't come up with a good idea.
I have the fallowing 3 types Project, User and Result. A user and a project can have multiple results. My problem now is that a result can be of multiple types (12 right now and that can change). I have no idea how i'm supposed to structure this the only things that this results have in common is a type and an Id and i want a table for each of the different 12 types.
So what i have until now is this.
Project
public class Project :ITypedEntity<ProjectType>
{
public int Id { get; set; }
public string Name { get; set; }
public string Nationality { get; set; }
public virtual ProjectType Type { get; set; }
public string TitleEn { get; set; }
public string TitleRo { get; set; }
public string Acronym { get; set; }
public string ContractNo { get; set; }
public virtual ActivityField ActivityField {get;set;}
public string SummaryEn { get; set; }
public string SumarryRo { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public decimal Value { get; set; }
public bool IsPartner { get; set; }
public ICollection<User> Team{get;set;}
public string Website {get;set;}
public ICollection<INamedEntity> Results;
}
Result
public class Result:ITypedEntity<ResultType>
{
public int Id { get; set; }
public string Name{get;set;}
public ResultType Type { get; set; }
}
User
public class User:Person
{
public int CNP { get; set; }
public virtual Faculty Faculty { get; set; }
public bool IsUSAMV {get;set;}
public virtual ICollection<INamedEntity> Results {get;set;}
public virtual ICollection<Project> Projects {get;set;}
}
I don't think this helps but i'll paste them anyway
public interface INamedEntity:IEntity
{
string Name{get;set;}
}
public interface ITypedEntity<TType>:INamedEntity{
TType Type { get; set; }
}
public interface IEntity
{
int Id { get; set; }
}
Update 1
Some results types
public class BookChapter:INamedEntity
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Book Book {get;set;}
public int PagesInChapter {get;set;}
public string Pagination {get;set;}
}
public class Book:INamedEntity
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Person> Authors { get; set; }
public int Year { get; set; }
public Publisher Publisher {get;set;}
public int NumberOfPages { get; set; }
public string Pagination { get; set; }
public int ISBN { get; set; }
}
public class Patent:ITypedEntity<PatentType>
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Person> Authors { get; set; }
public virtual Person Holder{get;set;}
public string Number{get;set;}
public virtual PatentType Type { get; set; }
}
Sorry if the question is not clear, i'll update it with any other information you need.
Thanks.
Table 1: Articles
Table 2: ArticleCategories
how do I represent the relationship between the two tables which is a 1->1 relationship:
I can do the following, but I'm not sure it's the correct way :
public class Article
{
public int ArticleIndex { get; set; }
public int Category { get; set; }
public Guid User { get; set; }
public int Parent { get; set; }
public int Level { get; set; }
public int Order { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateExpires { get; set; }
public bool Show { get; set; }
public string Title { get; set; }
public string TitleHtml { get; set; }
public string Content { get; set; }
public string ContentHtml { get; set; }
public string ShortTitle { get; set; }
public ArticleCategory Category { get; set; }
}
public class ArticleCategory
{
public int CategoryIndex { get; set; }
public string Name { get; set; }
}
By convention, Code First expects an Id property for each class/table. Then you can do something like this:
public class Article
{
public int Id { get; set; }
public int ArticleIndex { get; set; }
public int Category { get; set; }
public Guid User { get; set; }
public int Parent { get; set; }
public int Level { get; set; }
public int Order { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateExpires { get; set; }
public bool Show { get; set; }
public string Title { get; set; }
public string TitleHtml { get; set; }
public string Content { get; set; }
public string ContentHtml { get; set; }
public string ShortTitle { get; set; }
public int ArticleCategoryId { get; set; }
public virtual ArticleCategory ArticleCategory { get; set; }
}
public class ArticleCategory
{
public int Id { get; set; }
public int CategoryIndex { get; set; }
public string Name { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
Note the virtual keyword. EF Code First needs this so it can perform its magic behind the scenes.
Now, if you are working with an Article, you can get all it's category info by doing article.ArticleCategory, and if you have an ArticleCategory you can find out what article it refers to with articleCategory.Articles.Single().
For more info, see this article by Scott Gu:
http://weblogs.asp.net/scottgu/archive/2010/12/08/announcing-entity-framework-code-first-ctp5-release.aspx